In our daily life we add some numbers and for this addition we use “+” sign. I have a one question, Can it be possible without using plus sign we add numbers? the answer is Yes! we can add two numbers without using the plus (“+”) sign.

In the below program we use two different methods for addition of two numbers. First method add() in which we add two numbers a and b without using +. And second method is recursive method i.e. recursiveAdd().

Both methods are produce same output. Instead of using + operator we use & , ^, << this operators for addition. The & operator are bitwise & which is used in java for adding bitwise numbers in java. Bitwise numbers is binary numbers stored in a integer form.

Let’s see how to write addition program using &, ^ and << operators.

public class AddWithoutPlus
{
public static int add(int a, int b)
{
if (b > a)
{
int temp = b;
b = a;
a = temp;
}
int carry = 0;
while (b != 0)
{
carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
public static int recursiveAdd(int a,int b)
{
if (b == 0)
return a;
else
return recursiveAdd(a ^ b, (a & b) <<1);
}
public static void main(String[] args)
{
int a = 12;
int b = 34;
System.out.println(add(a, b));
System.out.println(recursiveAdd(b,a));
}
}

Output: output of the above program is as follow.

46
46
Addition without using plus sign

Both the methods used in this program produce the same output but only ways this two methods are write is different.

We also write the articles about the simple addition programs. If you want to know about that then see this article Addition of Two Numbers.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top