Java Program to Interchange two Numbers

« Previous Program Next Program »

Interchange Two Numbers

To interchange two numbers in Java Programming, make a variable say temp of the same type, place the first number to the temp, then place the second number to the first and place temp to the second.

Java Programming to Interchange two Numbers

Following Java Program ask to the user to enter the two number (i.e., value of A and B) to interchange and display the result on the screen:

/* Java Program Example - Interchange Two Numbers */
import java.util.Scanner;

public class JavaProgram
    public static void main(String args[])
        int a, b, temp;
        Scanner scan = new Scanner(;
        System.out.print("Enter Value of A and B :\n");
        System.out.print("A = ");
        a = scan.nextInt();
        System.out.print("B = ");
        b = scan.nextInt();
        temp = a;
        a = b;
        b = temp;
        System.out.print("Number Interchanged Successfully..!!\n");
        System.out.print("A = " +a);
        System.out.print("\nB = " +b);

When the above Java Program is compile and executed, it will produce the following output:

Java Program interchange two numbers

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:

Java Online Test

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.