Here's a program to swap two numbers using only two variables with explanation using Java Programming language.
Output of Program:
Enter the 1st no.(a): 5
Enter the 2nd no.(b): 4
The swaping no. is 4,5.
import java.io.*; class swap{ public static void main(String[]args) throws IOException{ int a,b; DataInputStream din=new DataInputStream(System.in); System.out.println("Enter the 1st no.(a): "); a=Integer.parseInt(din.readLine()); System.out.println("Enter the 2nd no.(b): "); b=Integer.parseInt(din.readLine()); a=a+b; b=a-b; a=a-b; System.out.println("The swaping no. is " +a+ "," +b+"."); } }
Output of Program:
Enter the 1st no.(a): 5
Enter the 2nd no.(b): 4
The swaping no. is 4,5.