Programming Examples

Java program to input two integers and interchange their values


Write a program to input two integers (say a and b) and interchange their values and display the result.

For Example:

INPUT: Enter two integers: 15 36

OUTPUT:

Before interchange: a=15 and b=36

After interchange: a=36 and b=15

Please note that you can take just one variable other than a and b for interchanging.

Solution

import java.util.*;
class Swap
{
	public static void main(String arr[])
	{
		Scanner sc=new Scanner(System.in);
		int a,b,c;
		System.out.println( “Enter 2 integers:”);
		a=sc.nextInt();
		b=sc.nextInt();
		System.out.println(“Before Interchange:a=”+a+“and b=”+b);
		c=a;
		a=b;
		b=c;
		System.out.println(“After Interchange:a=”+a+“and b=”+b);
	}
}
Output

Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2025 Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz