Answer to Question #72899 in Java | JSP | JSF for Atif Haroon

Question #72899
4. Write a program in Java to swap two numbers without using third variable.
For example if
a=5, and b=4 then after calling following function
Swap (int a, int b)
a=4 and b=5.
1
Expert's answer
2018-01-28T09:10:21-0500
public class Swap {
public static void main(String[] args) {
int a = 4;
int b = 5;
swap(a, b);
}

public static void swap(int a, int b) {
a=a+b;
b=a-b;
a=a-b;
System.out.println("a="+a+" and b="+b);

}
}


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS