Write a c program to add two numbers without using addition operator






Add two numbers in c without using operator

How to add two numbers without using the plus operator in c

#include<stdio.h>

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;
}



Sample output:

Enter any two integers: 5 10

Sum of two integers: 15


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





20 comments:

  1. sir (~) tiled use karne se two number add kyun hue...kya aap xplain kr sakte h...

    ReplyDelete
  2. @mohit
    a+b=a-(-b)
    but we know -b=(~b+1)
    so
    a+b=a - (~b+1)
    a+b= a - ~b - 1

    ReplyDelete
  3. I m just getting crazy in making c problems and i have completed all my assignments and problems that i had in handouts or pdf's and now i got this site as a helping tool to improve my skill to professional level..... Thankx man.. for giving us problems.... :) mean c problems.... ;)

    ReplyDelete
  4. here is a simple logic...adding a and b..say a=5 b=4 so a+b=5+4=9

    int main(void)
    {
    int a=5,b=4;// you can input those nos from user...
    while(b)
    {
    a++;
    b--;
    }
    printf("%d",a);
    }

    ReplyDelete
  5. still not clear with '~'...

    ReplyDelete
    Replies
    1. '~' is a type of bitwise operator.
      This simply means one's complement.
      Bitwise operators can only be operated upon ints & chars.
      On taking the one's complement of a number, all the 1's are changed to 0's and vice-versa.
      e.g: one's complement of 5 (0101) is -6 (1010).

      Delete
  6. ~b = -(b+1)
    so a-(~b)-1 = a-(-(b+1))-1
    = a+b+1-1
    = a+b

    ReplyDelete
  7. This code doesn't use any arithmetic operators to add two numbers.

    main()
    {
    int num1, num2;
    scanf("%d %d",num1, num2);
    printf("%d", Add(num1, num2));
    }

    int Add(int x, int y)
    {
    if (y == 0)
    return x;
    else
    return Add( x ^ y, (x & y) << 1);
    }

    ReplyDelete
    Replies
    1. please write explanation also. . .

      Delete
  8. while (num2) // do until carry
    {
    int carry = num1 & num2 // if its 1 & 1 we get carry
    num1 = num1 ^ num2; // add all 0 +1 = 1
    num2 = carry << 1; // now we need to carry
    }
    return num1

    ReplyDelete
  9. while (num2) // do until carry
    {
    int carry = num1 & num2 // if its 1 & 1 we get carry
    num1 = num1 ^ num2; // add all 0 +1 = 1
    num2 = carry << 1; // now we need to carry
    }
    return num1

    ReplyDelete
  10. while (num2) // do until carry
    {
    int carry = num1 & num2 // if its 1 & 1 we get carry
    num1 = num1 ^ num2; // add all 0 +1 = 1
    num2 = carry << 1; // now we need to carry
    }
    return num1

    ReplyDelete
  11. plz also share the problem for multiplication with out using * operator!!!

    ReplyDelete
  12. Here is a slight modification that adds subtraction feature as well:

    /* Adds two signed integers. Does a subtract when cin = 1 */
    int adder(int a, int b, int cin)
    {
    if (cin) b = ~b;
    int carry = (a & b) << 1 | cin;
    a ^= b;
    b = carry;

    while (b) {
    carry = (a & b) << 1;
    a ^= b;
    b = carry;
    }

    return a;
    }

    ReplyDelete
  13. you can as well use ^ operator.
    a^b will give you its sum.

    ReplyDelete
  14. void main()
    {
    int a=10,b=10,c;
    clrscr();
    c=a- -b;
    printf("%d",c);
    }

    ReplyDelete