Swap2.java
Aim:
To swap two numbers without using temporary or third variable
Source code:
import java.lang.*;
import java.util.*;
class Swap2
{
public static void main(String args[])
{
int a,b;
System.out.println("Enter values for a,b");
Scanner input=new Scanner(System.in);
a=input.nextInt();
b=input.nextInt();
System.out.println("Before swapping: a="+a+"b="+b);
a=a*b;
b=a/b;
a=a/b;
System.out.println("After swapping: a="+a+"b="+b);
}
}
Output:
Enter values for a,b
10
20
Before swapping: a=10b=20
After swapping: a=20b=10
No comments:
Post a Comment