Write a program to swap the values of three variables without using fourth variable.

 

public static void main(String[] args) {

Scanner n=new Scanner(System.in);

int a, b,c;

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

a= n.nextInt();

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

b=n.nextInt();

System.out.println(“Enter 3rd Values”);

c=n.nextInt();

a=a+b+c;

b=a-b-c;

c=a-b-c;

a=a-b-c;

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

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

System.out.println(“3rd value is=”+c);

}

}

output

Enter 1st value

4

Enter 2nd value

6

Enter 3rd value

9

1st value is=9

2nd value is=4

3rd value is=6

Leave a Reply

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