Sum two numbers without using "+" operator


Write code to sum 2 integer but u cant use a+b method, you have to use either ++ or --. How you will handle negative numbers.


Solution:

public class AddingNumbersWithoutPlusOperator {
public static void main(String[] args) {
int n=addNumbers(11,19);
System.out.println(n);
}
public static int addNumbers(int n1,int n2)
{
//Handling when two Integers are Positive
while(n1>0)
{
n2++;
n1--;
}
//Handling negative numbers Challenge Lies in handling the negative Integers
while(n1<0)
{
n1++;
n2--;
}
return n2;
}
}

Post a Comment