-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswap.java
More file actions
27 lines (24 loc) · 812 Bytes
/
Copy pathswap.java
File metadata and controls
27 lines (24 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// swap a 2 numbers
import java.util.Scanner;
public class swap {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 2 number to swap ");
int a = sc.nextInt();
int b = sc.nextInt();
int temp = a;
System.out.println("temp= " + temp);
a = b;
System.out.println("a " + a);
System.out.println("b " + b);
b = temp;
System.out.println("b " + b);
System.out.println("resylt1 = " + a + "," + b); // result of swap
System.out.println("without using temp or 3rd variable");
a = a + b;
b = a - b;
a = a - b;
System.out.println("result2 = " + a + "," + b); // again swaping the result from above swap
sc.close();
}
}