Saturday, February 14, 2015

Java program to swap two numbers without using temp variable

Java program to swap two numbers without using temp variable

class Swap
{
public static void main(String...s)
{
int a,b;
a=Integer.parseInt(s[0]);
b=Integer.parseInt(s[1]);

System.out.println("
Before Swap:
"+"a="+a+" b="+b);


a=a+b;
b=a-b;
a=a-b;

System.out.println("
After Swap:
"+"a="+a+" b="+b);

}
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.