Swapping of Two Numbers Without using Third Variable in Java language
ADVERTISEMENTS
Swapping of two numbers without using third variable in java language. In this article, you will learn how to swapping of two numbers without using third variable in java language.
Source Code
// Swapping of Two Numbers without using Third Variable in Java language
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double p, q;
System.out.println("----Enter the two integer or real numbers----");
p = in.nextFloat();
q = in.nextFloat();
p = p - q;
q = p + q;
p = q - p;
System.out.printf("\nAfter swapping, p = %.2f", p);
System.out.printf("\nAfter swapping, q = %.2f\n", q);
}
}
Output
----Enter the two integer or real numbers----
23
17
After swapping, p = 17.00
After swapping, q = 23.00