Swapping In Java Without Third Variable - In last tutorial we saw how to calculate in java addition, sub, multiple, division and swapping with third party variable as like empty or any named variable to swap value but today we will see
how to swap in java without third variable.
Swapping In Java Without Third Variable
Related:
How to Calculate with Javaimport java.io.*;
class swap{
public static void main(String args[]) throws IOException{
Integer value1;
Float value2,add,sub,mul,div,temp;
String s1,s2;
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(in);
System.out.print("Enter 1st number: ");
s1 = br.readLine();
System.out.print("Push 2nd number: ");
s2 = br.readLine();
value1 = Integer.valueOf(s1);
value2 = Float.valueOf(s2);
add = value1+value2;
sub = value1-value2;
mul = value1*value2;
div = value1/value2;
System.out.println("=========Output=========");
System.out.println("Addition of two number: "+add);
System.out.println("Subtraction of two number: "+sub);
System.out.println("Multiplication of two number: "+mul);
System.out.println("Division of two number: "+div);
temp = add;
add = div;
div = temp;
temp = sub;
sub = mul;
mul = temp;
System.out.println("=========After Swapping With Variable=========");
System.out.println("Addition of two number: "+add);
System.out.println("Subtraction of two number: "+sub);
System.out.println("Multiplication of two number: "+mul);
System.out.println("Division of two number: "+div);
add = add+div;
div = add-div;
add = add-div;
sub = sub+mul;
mul = sub-mul;
sub = sub-mul;
System.out.println("=========After Swapping Without Variable Method 1=========");
System.out.println("Addition of two number: "+add);
System.out.println("Subtraction of two number: "+sub);
System.out.println("Multiplication of two number: "+mul);
System.out.println("Division of two number: "+div);
add = add*div;
div = add/div;
add = add/div;
sub = sub*mul;
mul = sub/mul;
sub = sub/mul;
System.out.println("=========After Swapping Without Variable Method 2=========");
System.out.println("Addition of two number: "+add);
System.out.println("Subtraction of two number: "+sub);
System.out.println("Multiplication of two number: "+mul);
System.out.println("Division of two number: "+div);
}
}
and when you run this then it will print like this here I entered two value you can also input your own value and see the result.
0 Comments