Saturday, 13 September 2014

Swapping two numbers

java source code

import java.util.*;
class swap
{
  public static void main(String[] args)
   {
     Scanner scr=new Scanner(System.in);
     System.out.print("Enter first number:");
     int n1=scr.nextInt();
     System.out.print("Enter second number:");
     int n2=scr.nextInt();
System.out.println("The first number is "+n1+" and the second number is "+n2);
       /*logic for swapping,
       1>assign number1 to a temporary variable
       2>assign number2 to number1
       3>assign temporary variable to number1*/
     int temp=n1;
     n1=n2;
     n2=temp;
System.out.print("After swapping, the first number is "+n1+" and the second number is "+n2);
  }
}

No comments:

Post a Comment