Tuesday 5 February 2019

12. Swapping of two Numbers in java without third variable.


Description:
Write a sample code to Swapping of two Numbers in java without third variable.


Code:
?
1
2
3
4
5
6
7
8
9
10
import java.util.Scanner; public class Swapping_Number { public static void main(String[] args) { int x,y; System.out.println("Enter x and y no: "); Scanner s=new Scanner(System.in); x=s.nextInt(); y=s.nextInt(); System.out.println("Before Swaping \n x="+x+"\n y="+y); x=x+y; y=x-y; x=x-y; System.out.println("After Swaping\n x="+x+"\n y="+y); } }


Output:
Enter x and y no: 
10
20
Before Swaping 
 x=10
 y=20
After Swaping
 x=20
 y=10

No comments:

Post a Comment