Java program to swap two numbers ?

This java program swaps two numbers using a temporary variable.


								
import java.util.Scanner;

/**
 *
 * @author Mindclues
 */

class SwapNumbers
{
   public static void main(String args[])
   {
      int a, b, temp;
      System.out.println("Enter two numbers a and b :");
      Scanner sc = new Scanner(System.in);
 
      a = sc.nextInt();
      b = sc.nextInt();
 
      System.out.println("Before Swapping numbers value : \na = "+a+"\nb = "+b);
 
      temp = a;
      a = b;
      b = temp;
 
      System.out.println("After Swapping numbers value : \na = "+a+"\nb = "+b);
   }
}
							

								
Enter two numbers a and b :
8
6
Before Swapping numbers value : 
a = 8
b = 6
After Swapping numbers value : 
a = 6
b = 8

							

Related Articles

post a comment