Monday, 5 January 2015

Java Program to performing Swapping of 2 numbers


This is a Java program which swaps the given 2 numbers . Here we use a temporary variable to perform swapping.

PROGRAM :
package codingcorner.in;

import java.util.Scanner;

public class Swapping {
 public static void main(String[] args) {
  int number1, number2, num;
  Scanner scan = new Scanner(System.in);
  System.out.println("Enter first number to swap:\t");
  number1 = scan.nextInt();
  System.out.println("Enter Second number to swap:\t");
  number2 = scan.nextInt();

  scan.close();
  System.out.println("The two numbers before swapping are " + number1+ "\t" + number2);

  num = number1;
  number1 = number2;
  number2 = num;

  System.out.println("The two numbers before swapping are " + number1+ "\t" + number2);
 }
}


OUTPUT :

No comments:

Post a Comment