Java Examples
Java ExamplesPrograms with output

Java Program to Swap Two Numbers

import java.util.Scanner;

public class SwapNumbers
	public static void main(String args[])
		int num1, num2, swap;
		Scanner scan = new Scanner(;
		System.out.print("Enter First Number: ");
		num1 = scan.nextInt();
		System.out.print("Enter Second Number: ");
		num2 = scan.nextInt();
		swap = num1;
		num1 = num2;
		num2 = swap;
		System.out.print("after Swapping\n");
		System.out.print("First Number: " +num1);
		System.out.print("\nSecond Number: " +num2);
Enter First Number: 10
Enter Second Number: 20
after Swapping
First Number: 20
Second Number: 10

Share this page on:

Was this page helpful ?

Let us know how we did!

Subscribe Email Updates

to get latest update