Swapping two numbers means inter changing the container of certain values.
import java.util.Scanner;
public class SwapTwoNumbers {
public static void main(String[] args) {
int no1, no2;
Scanner sc = new Scanner(System.in);
System.out.println("Enter two numbers to swap : ");
no1 = sc.nextInt();
no2 = sc.nextInt();
System.out.println("Number One before Swap : "+no1);
System.out.println("Number Two before Swap : "+no2);
no1 = no1+no2;
no2 = no1-no2;
no1 = no1-no2;
System.out.println("Number One After Swap : "+no1);
System.out.println("Number Two After Swap : "+no2);
}
}
Comments
Post a Comment