Skip to content

Commit e03decf

Browse files
committed
Adding two numbers without using + operator
1 parent 31ab800 commit e03decf

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.techgig.java;
2+
3+
import java.util.Scanner;
4+
5+
public class AddWithoutUsingPlus {
6+
public static void main(String[] args) {
7+
Scanner scanner = new Scanner(System.in);
8+
int a = Integer.parseInt(scanner.nextLine().trim());
9+
int b = Integer.parseInt(scanner.nextLine().trim());
10+
11+
while (b != 0) {
12+
int carry = a & b;
13+
a = a ^ b;
14+
b = carry << 1;
15+
}
16+
System.out.print(a);
17+
}
18+
}

0 commit comments

Comments
 (0)