Write a Java program to swap two numbers

Swapping two numbers is a basic yet important concept in programming. It can be done using various methods. Here, we’ll explore a simple way to swap two numbers in Java using a temporary variable. Let’s write a Java program to swap two numbers.

Java Program to Swap Two Numbers

Here’s a simple Java program to swap two numbers using a temporary variable:

import java.util.Scanner;

public class SwapNumbers {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);
        
        // Prompt the user to enter the first number
        System.out.print("Enter the first number: ");
        int firstNumber = scanner.nextInt();
        
        // Prompt the user to enter the second number
        System.out.print("Enter the second number: ");
        int secondNumber = scanner.nextInt();
        
        // Print the original values
        System.out.println("Before swapping:");
        System.out.println("First number = " + firstNumber);
        System.out.println("Second number = " + secondNumber);
        
        // Swap the numbers using a temporary variable
        int temp = firstNumber;
        firstNumber = secondNumber;
        secondNumber = temp;
        
        // Print the swapped values
        System.out.println("After swapping:");
        System.out.println("First number = " + firstNumber);
        System.out.println("Second number = " + secondNumber);
    }
}

Explanation

Let’s break down the program to understand how it works:

  1. Importing the Scanner Class: We import the Scanner class from the java.util package to read input from the user.
import java.util.Scanner;

Creating the Main Class: We create a public class named SwapNumbers. In Java, the class name should match the filename.

public class SwapNumbers {

Main Method: The main method is the entry point of the program. Inside this method, we will write the code to swap two numbers.

public static void main(String[] args) {

Creating a Scanner Object: We create a Scanner object to read input from the user.

Scanner scanner = new Scanner(System.in);

Reading Input from the User: We prompt the user to enter the first and second numbers and store these values in variables.

System.out.print("Enter the first number: ");
int firstNumber = scanner.nextInt();

System.out.print("Enter the second number: ");
int secondNumber = scanner.nextInt();

Printing Original Values: We print the original values of the numbers before swapping.

System.out.println("Before swapping:");
System.out.println("First number = " + firstNumber);
System.out.println("Second number = " + secondNumber);

Swapping the Numbers: We use a temporary variable to swap the values of the two numbers.

int temp = firstNumber;
firstNumber = secondNumber;
secondNumber = temp;

Printing Swapped Values: We print the values of the numbers after swapping.

System.out.println("After swapping:");
System.out.println("First number = " + firstNumber);
System.out.println("Second number = " + secondNumber);

Running the Program

To run the program, follow these steps:

  1. Save the code in a file named SwapNumbers.java.
  2. Open a command prompt or terminal and navigate to the directory where you saved the file.
  3. Compile the program using the following command:
javac SwapNumbers.java

Run the compiled program using the following command:

java SwapNumbers
  1. Follow the prompts to enter the first and second numbers. The program will display the values before and after swapping.

What did we learn from this article?

This simple program demonstrates how to swap two numbers in Java using a temporary variable. It’s a fundamental exercise that helps beginners understand basic input/output operations, variable manipulation, and the use of the Scanner class in Java. By mastering these basic concepts, you can build a strong foundation for more advanced programming tasks.

Leave a Comment