-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE8_swappingtwonumbers.java
More file actions
62 lines (39 loc) · 1.26 KB
/
Copy pathE8_swappingtwonumbers.java
File metadata and controls
62 lines (39 loc) · 1.26 KB
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package src.Programs;
import java.util.Scanner;
public class E8_swappingtwonumbers {
public static void main(String[] args)
{
//swapping two numbers
Scanner sc = new Scanner(System.in);
System.out.println("Enter the two numbers");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int temp;
temp = num1;
num1 = num2;
num2 = temp;
System.out.println("swap of two number is "+ num1 +" "+ num2);
//swapping two numbers without adding two numbers
Scanner sc1 = new Scanner(System.in);
System.out.println("Enter the two numbers");
int n1 = sc1.nextInt();
int n2 = sc1.nextInt();
n1 = n1 + n2; // a= a+b; b=a-b; a=a-b; a= 10, b=20
n2 = n1 - n2; // a= 30 b=20 a=10
n1 = n1 - n2;
System.out.println("swap without third num "+ n1 +" "+ n2);
//another method without using third variable --> if we give any one of the value as (zero) it will not work
int a =44;
int b =23;
a = a*b;
b = a/b;
a = a/b;
System.out.println(a+" "+b);
//bitwise xor (not recommended)
/*
a = a^b;
b = a^b;
a= a^b;
*/
}
}