How to add 2 numbers without + sign?

How to add 2 numbers without + sign?

I have given you some examples.Which add 2 numbers with using + sign.
 
Using recursion:

#include 

int add(int m, int n)
{
  if (!m) 
  return n;
  else
  return add((m & n) << 1, m ^ n);
}
int main()
{
   int m,n;
   printf("Enter the 2 numbers: \n");
   scanf("%d",&m); 
   scanf("%d",&n);
   printf("Addition is: %d",add(m,n));
}
m ^ n is mandatry in addition of bits, "(a & b) << 1" is the overflow. 

Using Binary operator:
1 = 001
2 = 010
add(001, 010) => a -> 001, b-> 010
=011

By:Vivek Kr. Agarwal
Date:2002-01-09 00:00:00

void AddTwoNumbers(int nNumber1,int nNumber2){
int nTemp=0,nSum=0;
if(nNumber2 > iNumber1)
{
    nTemp=nNumber2-nNumber1;
    nNumber2=nNumber2*2;
    nSum=nNumber2-nTemp;
}else
{
    nTemp=nNumber1-nNumber2;
    nNumber1=nNumber1*2;
    nSum=nNumber1-nTemp;
}
}

By:Mukeshsale
Date:2016-11-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related C Subjective Links

C Subjective interview questions and answers for experienced and fresher