ccplusplus.com
Learn C, C++ Concepts
Friday, January 13, 2012
add two number without using add operator in c
/*********************************************************************** * File : sum-without-add-operator.c * Author : Saurabh Gupta * Desc : add two number without using add operator in c * Source : http://ccplusplus/p/c.html * Created : PM 06:48 13 January 2012 * 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 ************************************************************************/ #include
int main() { int a, b; int sum; printf ("Enter first number : "); scanf ("%d", &a); printf ("Enter second number: "); scanf("%d", &b); sum = a - ~b -1; printf("Sum of two integers: %d\n",sum); return 0; } /* * OUTPUT * [sgupta@rhel6x64 c]$ gcc sum-without-add-operator.c -o sum-without-add-operator [sgupta@rhel6x64 c]$ ./sum-without-add-operator Enter first number : 10 Enter second number: 20 Sum of two integers: 30[sgupta@rhel6x64 c]$ */
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment