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

 

public static void main(String[] args) {

Scanner n=new Scanner(System.in);

int a, b,c,d;

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();

d=a;

a=b;

b=c;

c=d;

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

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

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

 

}

}

Output:

Enter 1st Values

23

Enter 2nd Values

45

Enter 3rd Values

57

1st value is=56

2nd value is=57

3rd value is=23

Leave a Reply

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