14. JAVA Program to Interchange Contents in Two variables Without Using Temporary Variable.
How does this program work?
- In this programme you will learn how to easily Interchange contents in Two variables without using Temperory variable.
- Here we take only two varibles and store them in A and B variables.
- This programme perform easy mathematical calculations and display the result using java.
Here is the code
//Interchanging two numbers without using Temperory variable
import java.util.Scanner;public class Variable
{
public static void main(String args[])
{
System.out.println("Enter the value of x and y");
Scanner obj = new Scanner(System.in);
/*Define variables*/
int x = obj.nextInt();int y = obj.nextInt();
System.out.println("Before swapping : "+x +" "+ y);
/*Swapping*/
x = x + y;y = x - y;
x = x - y;
System.out.println("After swapping: "+x +" " + y);
}
}