-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdd_Without_Operators.java
More file actions
41 lines (23 loc) · 1.17 KB
/
Copy pathAdd_Without_Operators.java
File metadata and controls
41 lines (23 loc) · 1.17 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
import java.util.Scanner;
import java.util.Scanner;
public class Add_Without_Operators{
public static void main(String[] arg) {
int x, y;
// Declare two integer variables, 'x' and 'y'
Scanner in = new Scanner(System.in);
// Create a Scanner object for user input
System.out.print("Input first number: ");
// Prompt the user to input the first number
x = in.nextInt();
// Read and store the first number from the user
System.out.print("Input second number: "); // Prompt the user to input the second number
y = in.nextInt(); // Read and store the second number from the user
while (y != 0) {
int carry = x & y; // Calculate the carry by bitwise AND operation between x and y
x = x ^ y; // Calculate the sum without considering the carry by bitwise XOR operation
y = carry << 1; // Calculate the carry for the next iteration by shifting it left by one position
}
System.out.print("Sum: " + x); // Print the sum of the two numbers
System.out.print("\n"); // Print a new line
}
}