Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Java-conversion Swap two any numbers

Related Tutorials


 
 

Share on Google+Share on Google+

Swap two any numbers

Advertisement
This is a simple Java Oriented language program. If you are newbie in Java programming then our tutorial and example are helpful for understanding Java programming in the simplest way.

Swap Any Two Numbers

     

This is a simple Java Oriented  language program. If  you are newbie in Java programming then our tutorial and example are helpful for understanding Java programming in the simplest way.  Here we will learn how to swap or exchange the number to each other. First of all we have to create a class "Swap". Now, we use the Integer.parseInt(args[o]) and Integer.parseInt(args[1]) methods for getting the integer type values in command line. Use a temporary variable z of type integer that will help us to swap the numbers  Assign the value of x to z variable, now assign the value of y to the variable x. Lastly assign the value of z to variable y. Now we get the values has been interchanged. To display the values on the command prompt use println() method and the swapped values will be displayed.

Here is the code of this program

public class Swap {
  public static void main(String[] args) {
  
  int x= Integer.parseInt(args[0]);
  int y= Integer.parseInt(args[1]);
  System.out.println("Initial value: "+ x +" and " + y);
  int z = x;
  x = y;
  y = z;
  System.out.println("Swapped value: " + x +" and " + y);
  }
}

Download this Example. 

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 25, 2008

Related Tutorials

Discuss: Swap two any numbers   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com