In this program we will see how to add two numbers without using arithmetic operators in java . We can use bitwise operators like & , ^ and << . Now we will write program for sum two numbers without using arithmetic operators(+ , – , % )
public class AddTwoNumbersWithoutArithmeticOperator {
public static void main(String[] args)
{
int a =25;
int b =25;
System.out.println("sub of "+a+" and "+b+" ="+addingTwoNumber(a , b));
}
static int addingTwoNumber(int a, int b)
{
while(b != 0)
{
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
}
Output :
sub of 25 and 25 =50
In this program we have learned how to add two numbers without using arithmetic operators in java or addition of two numbers without using + operator . In this program we have add two numbers using bitwise operators (& , ^ and <<). You can see Top 50 Core Java Coding / Programming Questions And Answers and Top string coding question .