Skip to content

Commit eb1948a

Browse files
Merge pull request #36 from Ln11211/swap
Created SWAP program
2 parents 972cb8b + cdc1cd8 commit eb1948a

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

Basic Codes/swap2.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+"\ny= "+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+"\ny= "+y);
29+
swap2 obj=new swap2();
30+
obj.swap(x,y);
31+
}
32+
}

0 commit comments

Comments
 (0)