Java Program to Swap Two Numbers

Java Program to Swap Two Numbers
Reading Time: 6 minutes

Introduction:

The program is designed to swap two numbers entered by the user using the console. It uses the Scanner class to take input from the user and employs a simple swapping logic to interchange the values of the two numbers.

Algorithm :

  1. Import Scanner Class:

This line imports the Scanner class from the java.util package, allowing us to take user input.

  1. Main Class Declaration:

The program is defined within a class named SwapNumbers.

  1. Main Method:

The execution of the program begins with the main method.

  1. User Input:

The program prompts the user to enter two numbers, which are then stored in variables num1 and num2.

  1. Display Before Swapping:

This section prints the values of the two numbers before swapping.

  1. Swapping Logic:

The values of num1 and num2 are swapped using a temporary variable temp.

  1. Display After Swapping:

Finally, the program prints the values of the two numbers after the swapping operation.

Explanation :

The program begins by taking two integer inputs from the user, displaying the values before swapping, performing the swap using a temporary variable, and then displaying the values after the swap. The swapping logic involves storing the value of num1 in a temporary variable, assigning the value of num2 to num1, and then assigning the temporary value to num2. This way, the values of the two numbers are effectively swapped.

Code :

    
     import java.util.Scanner;
public class SwapNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter the first number: ");
        int num1 = scanner.nextInt();
        System.out.print("Enter the second number: ");
        int num2 = scanner.nextInt();
        System.out.println("Before swapping: ");
        System.out.println("First number: " + num1);
        System.out.println("Second number: " + num2);
        // Swapping logic
        int temp = num1;
        num1 = num2;
        num2 = temp;
        System.out.println("After swapping: ");
        System.out.println("First number: " + num1);
        System.out.println("Second number: " + num2);
    }
}

    
   

Input :

    
     Enter the first number: 65
Enter the second number: 95
    
   

Output :

    
     Before swapping: First number: 65
Second number: 95
After swapping: 
First number: 95
Second number: 65
    
   

This program takes two numbers as input from the user, swaps them using a temporary variable, and then prints the numbers before and after swapping.

FAQs :

Q1: What is the basic structure of a Java program to swap two numbers?

A1: In Java, a simple program to swap two numbers involves the use of a temporary variable.

Q2: Can you provide a Java program to swap two numbers without using a temporary variable?

A2: Certainly! Here’s an example of swapping two numbers in Java without using a temporary variable:

Q3: How can I write a Java program to swap two numbers using arithmetic operators?

A3: You can achieve swapping using arithmetic operators as shown in the previous example. Addition and subtraction operations are utilized to swap the values without using a temporary variable.

Q4: Is there a way to swap two numbers in Java using the XOR bitwise operation?

A4: Yes, you can swap two numbers using the XOR bitwise operation.

Q5: Can I input values from the user for swapping in a Java program?

A5: Absolutely! You can modify the Java programs to accept user input. Use the Scanner class to take input from the user for the numbers to be swapped.