Examples
- Hello World Program in Java (Print Hello World in Java)
- Java Program to Add Two Numbers (Java Sum / Addition)
- Find Greatest of Three Numbers in Java (Largest Number Program)
- Prime Number Program in Java (Code to Check Prime or Not)
- Java Program for Fibonacci Series (Using for, while, recursion, scanner)
- Factorial Program in Java (Find Factorial of a Number in Java)
- How to Find Sum of Digits of a Number in Java?
- How to Reverse a Number in Java? Program & Examples
- How to Swap Two Numbers in Java? Programs With/Without Third Variable
- Even Odd Program in Java (Program to Check Number is Even or Odd)
- Vowel and Consonant Program in Java
- Java Program for Quadratic Equation (Find Roots With 3 Ways)
- Find Frequency of Characters in a String in Java (4 Ways)
- Remove Space from String in Java (Remove Whitespace)
- String Null Check in Java (String is Empty or Null) - 5 Ways
- How to Print String in Java? 6 Methods
- How to Get ASCII Value of Char in Java? Find ASCII Value
How to Swap Two Numbers in Java? Programs With/Without Third Variable
Let’s learn and practice how to swap two numbers in Java with different approaches!
Swapping of two numbers involves exchanging their values, a fundamental operation in programming that finds applications in sorting algorithms, data manipulation, and various mathematical computations.
Learning different techniques to swap two numbers in Java is crucial for writing robust and optimized code. Each technique offers unique insights into handling variables and their values, enabling you to choose the most appropriate approach for different scenarios and enhance your problem-solving skills.
So, let’s get started and write a program to swap two numbers in Java.
Java Program to Swap Two Numbers
Here's a Java program to swap two numbers using a temporary variable:
Code
mport java.util.Scanner;
public class NumberSwapper {
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);
swapNumbers(num1, num2);
System.out.println("After swapping: ");
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
}
private static void swapNumbers(int a, int b) {
int temp = a;
a = b;
b = temp;
}
}
Output
Enter the first number: 54
Enter the second number: 90
Before swapping:
First number: 54
Second number: 90
After swapping:
First number: 54
Second number: 90
Explanation
In this program, we use a method called swapNumbers to swap the values of two variables. The main method prompts the user to enter two numbers, and then it displays the values before swapping. After calling the swapNumbers method, it displays the values again to show the result after swapping.
The swapNumbers method takes two integers as input and uses a temporary variable temp to hold the value of the first number a. Then, it assigns the value of the second number b to a, and finally, it assigns the value of the temporary variable temp to b. This way, the values of the two variables are swapped.
Swap Two Numbers in Java Without Using Third Variable
Here's a program to swap two numbers without using third variable in Java:
Code
import java.util.Scanner;
public class NumberSwapper {
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);
swapNumbers(num1, num2);
System.out.println("After swapping: ");
System.out.println("First number: " + num1);
System.out.println("Second number: " + num2);
}
private static void swapNumbers(int a, int b) {
a = a + b;
b = a - b;
a = a - b;
}
}
Output
Enter the first number: 3
Enter the second number: 8
Before swapping:
First number: 3
Second number: 8
After swapping:
First number: 3
Second number: 8
Explanation
In this program, we have a swapNumbers method that takes two integers a and b as input. Instead of using a third variable, we use arithmetic operations to swap the values of a and b.
Here's how the swapping works without using a third variable:
-
Add a and b, and store the result in a.
-
Subtract the original value of b from the new value of a, and store the result in b. This effectively stores the original value of a in b.
-
Subtract the original value of b from the new value of a (which is the original value of a plus the original value of b), and store the result in a. This effectively stores the original value of b in a.
As a result, the values of a and b are swapped without using a third variable.
Learn Next: