Write a program that inputs two numbers, swaps these values without using third variable and display them.

 

public static void main(String[] args) {

Scanner n=new Scanner(System.in);

int a, b;

System.out.println(“Enter 1st Values”);

a= n.nextInt();

System.out.println(“Enter 2nd Values”);

b=n.nextInt();

a=a+b;

b=a-b;

a=a-b;

System.out.println(“1st value is=”+a);

System.out.println(“2nd value is=”+b);

}

}

Output

Enter 1st value

6

Enter 2nd  value

9

1st value is=9

2nd value is=6

Leave a Reply

Your email address will not be published. Required fields are marked *