File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ //A program to swap two numbers without using another variable.
2+ import java .io .*;
3+ class swap2
4+ {
5+ void swap (int x , int y )
6+ {
7+ if (x >=0 && y >=0 ) //If the numbers are both positive..
8+ {
9+ x =x +y ;
10+ y =x -y ;
11+ x =x -y ;
12+ }
13+ else //if any of the number is negative or both are negative..
14+ {
15+ x =x +y ;
16+ y =y -x ;
17+ x =x +y ;
18+ y =-y ;
19+ }
20+ System .out .println ("After swap \n " +"x= " +x +"\n y= " +y );
21+ }
22+ public static void main (String args [])throws IOException
23+ {
24+ InputStreamReader inp =new InputStreamReader (System .in );
25+ BufferedReader br =new BufferedReader (inp );
26+ int x =Integer .parseInt (br .readLine ()); // Accepting the numbers and storing them
27+ int y =Integer .parseInt (br .readLine ());
28+ System .out .println ("Before swap \n " +"x= " +x +"\n y= " +y );
29+ swap2 obj =new swap2 ();
30+ obj .swap (x ,y );
31+ }
32+ }
You can’t perform that action at this time.
0 commit comments