webisworld



Webisworld Edit Try It Editor
Share This Page :

temp image

Java Program to Swap Two Numbers


Example:

import java.util.Scanner;

public class SwapNumbers
{
	public static void main(String args[])
	{
		int num1, num2, swap;
		Scanner scan = new Scanner(System.in);
		
		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);
	}
}

Output:

Enter First Number: 10
Enter Second Number: 20
after Swapping
First Number: 20
Second Number: 10

Share This Page :

Was this page helpful?

Let us know how we did