ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

How to swap two numbers in Java passed from console ?

Updated on September 17, 2014

The following code is a simple Java program which swaps two numbers. It reads two integers from the console. After swapping, it prints result on the console. This is just a simple program which illustrates working of many features of Java programming language. In this application, there is one class named as 'SwapDemo'. This class contains a method named as main. It’s the starting point for any application to execute. I will also provide you with the source code below. Here is the code :

package com.hubpages.swap.example;

import java.util.Scanner;

public class SwapDemo {

	public static void main(String[] args) {

		// Create a Scanner object which will read 
		// values from the console which user enters
		Scanner scanner = new Scanner(System.in);

		System.out.println("Enter value of number 1 ");
		int num1 = scanner.nextInt();

		System.out.println("Enter value of number 2 ");
		int num2 = scanner.nextInt();

		// Lets print the value of two numbers before 
		// swapping them 
		System.out.println("Before Swapping of the numbers : ");

		System.out.println("Value of num1 is :" + num1);
		System.out.println("Value of num2 is :" +num2);

		// passing the values to a static method which will
		// perform the swapping of number for us 
		swapNumbers(num1, num2);
	}

	private static void swapNumbers(int number1, int number2) {

		// We will create a temp variable which will store the one of 
		// the value for us say either number1 or number2
		int temp = number1;

		// number1 value is stored into the temp variable, so its stored
		// somewhere in the variable, we now store value of number2 in
		// to number1 for performing the swapping
		number1 = number2;

		// Finally, we store the value of temp into variable 
		// number2 thus reversing the numbers 		
		number2 = temp;

		// Lets print the value of two numbers after 
		// swapping them 
		System.out.println();
		System.out.println("After Swapping of the numbers : ");
		System.out.println("Value of num1 is :" + number1);
		System.out.println("Value of num2 is :" +number2);

	}
}

Steps to implement

Step 1 :- Create a Scanner object which will read values from the console which user enters.

Step 2 :- Prompt user to enter number1 and number2.

Step 3 :- Print the numbers before swapping them.

Step 4 :- Passing the values to a static method which will perform the swapping of numbers.

Step 5 :- In the swapNumbers method, We will create a temp variable which will store the one of the value for us say either number1 or number2.

Step 6 :- number1 value is stored into the temp variable, so its stored somewhere in the variable, we now store value of number2 into number1 for performing the swap.

Step 7 :- Finally, we store the value of temp into variable number2 thus reversing the numbers.

Step 8 :- Print the numbers on the console after swapping.

 

Video Tutorial : Swapping of two numbers in Java

Output of the program :

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    Click to Rate This Article