Java Program to Swapping Two Numbers without Using a Temporary Variable

In this tutorial you will learn about the Java Program to Swapping Two Numbers without Using a Temporary Variable and its application with practical example.

Java Program to Swapping Two Numbers without Using a Temporary Variable

In this tutorial, we will learn to create a Java program that will be Swapping Two Numbers without using a Temporary Variable in Java programming.

Prerequisites

Before starting with this tutorial, we assume that you are the best aware of the following Java programming topics:

  • Operators in Java Programming.
  • Basic Input and Output function in Java Programming.
  • Basic Java programming.
  • Arithmetic Operations in Java Programming.

Algorithm:-

Swapping Two Numbers without using a Temporary Variable.

In today’s tutorial, we will learn to create a java program that will swap two numbers. First, we will take the numbers in input from the user. Then we will pass that number to the arithmetic equations to swap the numbers without the third variable. At last, we will print the swapped numbers to the user using the print function.

With the help of this program, we can Swap Two Numbers without using a Temporary Variable.

Program Code:-

Output:-

In the above program, we have first initialized the required variable.

  • x = it will hold the integer value.
  • y = it will hold the integer value.

Taking input numbers from the user.

Swapping the numbers without using the third variable in the program.

Printing the numbers after swapping.

In this tutorial we have learn about the Java Program to Swapping Two Numbers without Using a Temporary Variable and its application with practical example. I hope you will like this tutorial.