Home Java Beginners Swap two any numbers (from Keyboard)



Swap two any numbers (from Keyboard)
Posted on: March 12, 2008 at 12:00 AM
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 two any numbers (from Keyboard)

     

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. 

Related Tags for Swap two any numbers (from Keyboard):
javacprogrammingcomclassstllanguagemethodsparsehelpexchangeintegermethodvariableprintgettypedisplaycommandnumberstutorialnewvaluenumberchangeintlineriathisappsimplecreatepromptifforieexampleswapprogramnewbievaluestolearnramexameachwaptemporaryastlanssiearbieilsignlastcommalihangsplvaruseulpeimmanfirstinnorarasstamntplaypartempminminlnjispclesemargsallagercmehowpropptorxaxampsspeeatisirhallmplparseinteaandarsimunderstandingvatttutorzsimplestssrirgstempothswavstabablapaluhatdispintlpleplprmindodsonomogro


More Tutorials from this section

Ask Questions?    Discuss: Swap two any numbers (from Keyboard)   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.