Write a program that ask user to enter values in two integer a and b, now write a code to swap the values of a and b without taking any temporary variable.

import java.util.Scanner;
public class Swap
{
public static void main(String[]hjk)
{
int a,b;
Scanner s1=new Scanner(System.in);
System.out.println("enter first num as a");
a=s1.nextInt();
Scanner s2=new Scanner(System.in);
System.out.println("enter second num as b");
b=s2.nextInt();
a=a+b;
b=a-b;
a=a-b;
System.out.println("the swapped num are");
System.out.println("a="+a);
System.out.println("b="+b);

}
}

No comments:

Post a Comment