Java Program to Add two Numbers without using Arithmetic Operator

Arithmetic operators such as +, -, *, / are used to perform mathematical operations like addition, subtraction, multiplication, modulo and division. We all have done addition of two numbers using + operator but in this article, we are going to see a few java programs that can add two numbers without using arithmetic operators. The following operators best serve this purpose −

• Increment operators (++) − They are used to increment value of a variable by 1. It is of two types pre increment and post increment. Pre increment operator first increases the value and then, uses it for operation but post increment operator first uses it for operation and then increases the value of it. For this program, we can use either of these operators.

• Bitwise operators − These operators first convert the operands into their binary form and then perform the given operation. We use bitwise AND(&), XOR(^) and Left-Shift operator(<<).

Approach 1: Using Increment Operator

When we use ++ operator as prefix then, it became pre increment opearator. When we use it as postfix, it became post increment operator.

Example 1

We will understand the difference between pre increment and post increment operators through this example. We are post incrementing at 5th line therefore, value of ‘num’ is incremented to 10 but not assigned to ‘num2’ and we are getting value of ‘num2’ as 9. We can see the increased value of ‘num’ in next line. At the 8th line, value of ‘num’ will be increased to 11 because of pre increment operation. Hence, we are getting value of ‘num3’ as 11 in the next line.

import java.util.*;
public class Main{
public static void main(String[] args){
int num = 9;
int num2 = num++; // post increment
System.out.println("Value of num2: " + num2);
System.out.println("Value of num after post increment: " + num);
int num3 = ++num; // pre increment
System.out.println("Value of num3: " + num3);
}
}


Output

Value of num2: 9
Value of num after post increment: 10
Value of num3: 11


Example

import java.util.*;
public class Main {
public static int sum(int n1, int n2) {
for(int i=1; i<=n2; i++) {
n1++;
}
return n1;
}
public static void main(String[] args) {
System.out.println("Sum = " + sum(23,5));
}
}


Output

Sum = 28


In the above program, we have created an user-defined method ‘sum’ along with two parameters ‘n1’ and ‘n2’ of type integer. In that method, we incremented the value of ‘n1’ till ‘n2’ to find the sum of given numbers. In the main method, we have called the ‘sum’ with two arguments (n1 = 23, n2 = 5).

Approach 2: Using Bitwise Operators

Bitwise AND checks if both bits of operands are 1 then it returns 1 otherwise 0.

Truth table of AND

a

b

a&b

0

0

0

0

1

0

1

0

0

1

1

1

Bitwise XOR returns 1 if both bits are opposite otherwise 0.

Truth table of XOR −

a

b

a^b

0

0

0

0

1

1

1

0

1

1

1

0

Bitwise left-shift operator move bits of operand to the left by specified position.

x<<n, x will be converted to binary form and then, its bits will get shifted to number of position specified by n.

Algorithm

• Step 1 − We create a user-defined method ‘sum’ along with two parameters ‘n1’ and ‘n2’ of type integer. The while loop inside this method will run till ‘n2’ not equal to 0.

• Step 2 − We perform ‘&’ operation between 9 and 6 and assign the result to carry variable ‘c’ in the form of binary.

• Step3 − Now, we perform XOR operation so that the value of ‘n1’ will get updated to 15(1111 in binary). Since, value of ‘n2’ will become 0 after left shift operation, the while loop will stop its execution and 15 will be printed as output.

Example

import java.util.*;
public class Main {
public static int sum(int n1, int n2) {
while(n2!=0){
int c= n1 & n2; // n1 = 1001, n2 = 0110, c = 0000
n1= n1^n2; // n1 = 1111 after XOR operation
n2= c << 1; // n2 = 0000
}
return n1;
}
public static void main(String[] args) {
System.out.println("Sum = " + sum(9, 6));
}
}


Output

Sum = 15


Conclusion

In this article, we have performed addition of two numbers without using arithmetic operators. We have seen two approaches to do it. The first one is not suitable for adding two big numbers. The second approach is more faster and efficient way of doing it as compared to first because the bitwise operators work in the form of binary representation.

Updated on: 25-Apr-2023

842 Views