Write a Java-program to print swaping of two numbers without using third variable
Program
public class Main
{
public static void main(String args[])
{
int x=10,y=20,z;
x=x+y;
y=x-y;
x=x-y;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}
Output:
x value is: 20
y value is: 10
Using CommandLine Arguments
public class Main
{
public static void main(String args[])
{
int x,y,z;
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
x=x+y;
y=x-y;
x=x-y;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}
Output:
x value is: 20
y value is: 10
Using Scanner Class
import java.util.*;
public class Main
{
public static void main(String args[])
{
int x,y,z;
Scanner s=new Scanner(System.in);
System.out.println("Enter x,y values");
x=s.nextInt();
y=s.nextInt();
x=x+y;
y=x-y;
x=x-y;
System.out.println("x value is: "+(x));
System.out.println("y value is: "+(y));
}
}
Output:
Enter x,y values
10
10
x value is: 20
y value is: 10
More Questions
12 . Write a Java-program to print swaping of two numbers without using third variable
13 . Write a java-program to print relation of two numbers
14 . Write a Java-program to print logical operators program using &&, || ,!
15 . Write a Java-program to print bitwise AND operation
16 . Write a Java-program to print bitwise OR operation
17 . Write a Java-program to print bitwise CAP operation
18 . Write a Java-program to print bitwise NEGATION operation
19 . Write a Java-program to print bitwise LEFT SHIFT operation
20 . Write a Java-program to print bitwise RIGHT SHIFT operation
21 . Write a Java-program to print odd or even number using ternary operator
22 . Write a Java-program to print max number using ternary operator