c program to add two numbers without using addition operator

c program to add two numbers without using addition operator





#include

int main()
{

int a,b;
int sum;

printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

//sum = a - (-b);
sum = a - ~b -1;

printf("Sum of two integers: %d",sum);

return 0;
}



Well Friend the Algorithm of this program is as follows :


Algorithm:
In c ~ is 1's complement operator. This is equivalent to:
~a = -b + 1
So, a - ~b -1
= a-(-b + 1) + 1
= a + b – 1 + 1
= a + b


No comments:

Post a Comment