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





51 comments:

mohit said...

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

ankur patel said...

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

Anonymous said...

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.... ;)

Hanish Maghu said...

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

Anonymous said...

still not clear with '~'...

Anonymous said...

~b = -(b+1)
so a-(~b)-1 = a-(-(b+1))-1
= a+b+1-1
= a+b

Anand Barnwal said...

'~' 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).

Anand Barnwal said...

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

prashant said...

please write explanation also. . .

Mukilan said...

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

Mukilan said...

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

Mukilan said...

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

Anonymous said...

BH JUYHC

Anonymous said...

i cant understand

Anonymous said...

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

Yihuan Huang said...

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

Anonymous said...

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

joginder Banger said...

Good thing boss

Asrith Namgari Reddy said...

nice one

Vikram Mp said...

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

manan singla said...

You have used "+" operator (A++ which is A = A + 1) which is forbidden in the question itself.

Nancy Ncit said...

The mission of NCITSolutions is to provide viable, low cost outsourcing solutions. We offer you offshore services tailor made to suit your distinctive requirements. Our low cost IT solutions afford you a competitive edge while delivering tangible results. With a software team whose core competencies range from software design to graphic design, we are equipped to process all your IT needs as we operate on a vast gamut of systems such as: Linux, UNIX, Windows, iOS and Android.

NCITSolutions is an independent and unique organization that was created with the objective of facilitating offshore/outsourcing services to enable clients to benefit from potential business opportunities made available through the creation of free and fair markets in the Middle East region.

We offer:
• Individual outsourcing facilities uniquely tailored for each customer.
• Identification of available market resources and potential partners
• On-the-ground management and support in Jordan
• Legal, technical and cultural support for ventures.
Numerous global companies have set up offices in Amman- Jordan in a bid to promote partnerships and joint ventures with Middle East companies. NCITSolutions maintains a physical presence with staff on the ground in Amman- Jordan. We can also provide businesses with space and easy access to our business support services through our affiliate in Amman Jordan.

Visit us at ncitsolutions.com or contact us at 919-324-6505

Nancy Ncit said...

The mission of NCITSolutions is to provide viable, low cost outsourcing solutions. We offer you offshore services tailor made to suit your distinctive requirements. Our low cost IT solutions afford you a competitive edge while delivering tangible results. With a software team whose core competencies range from software design to graphic design, we are equipped to process all your IT needs as we operate on a vast gamut of systems such as: Linux, UNIX, Windows, iOS and Android.

NCITSolutions is an independent and unique organization that was created with the objective of facilitating offshore/outsourcing services to enable clients to benefit from potential business opportunities made available through the creation of free and fair markets in the Middle East region.

We offer:
• Individual outsourcing facilities uniquely tailored for each customer.
• Identification of available market resources and potential partners
• On-the-ground management and support in Jordan
• Legal, technical and cultural support for ventures.
Numerous global companies have set up offices in Amman- Jordan in a bid to promote partnerships and joint ventures with Middle East companies. NCITSolutions maintains a physical presence with staff on the ground in Amman- Jordan. We can also provide businesses with space and easy access to our business support services through our affiliate in Amman Jordan.

Visit us at ncitsolutions.com or contact us at 919-324-6505

Roopam Gupta said...

Simply we can do it as:
#include

int main(){

int a,b;


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

for(int i=0;i<b;i++)
{
a++;
}

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

return 0;
}

suraj kumar said...

+ is different and ++ is different... question says without using addition operator.. ++ is not addition operator. so the solution is correct. :)

ranielli anschau said...

^ is a bitwise operator and used to perform bit-by-bit operation.
C Tutorial

Krishna said...

~b = -(b + 1 )
This is the right ones complement .

sandeep saxena said...

I have clarified my doubts through this. Everyone will benefit and thankyou so much for this article.
core java training in chennai
core java course
core java Training in Adyar
c c++ course fees in chennai
javascript training center in chennai
Drupal Training in Chennai
Photoshop Classes in Chennai
core java training in chennai

Yogesh said...

Awesome post. Thank you so much.
ios app Devlopment company in chennai

raneedevan said...

nice blog..valuable information....thanks for sharing...
C C++ Training in Chennai
C Training in Chennai
C++ Training in Chennai
C C++ training in anna nagar
C C++ training in velachery
javascript training in chennai
core java training in chennai
Html5 Training in Chennai
DOT NET Training in Chennai
QTP Training in Chennai

vinudevan said...

Thanks for giving excellent Message. Waiting for the next article
DOT NET Training in Chennai
DOT NET Course Chennai
dot net institute in chennai
best dotnet training in chennai
dot net training in Porur
Html5 Training in Chennai
Spring Training in Chennai
Struts Training in Chennai
Wordpress Training in Chennai
SAS Training in Chennai

Riya Raj said...

Wonderful blog!!! Thanks for sharing this great information with us...
SEO Training in Chennai
SEO Course in Chennai
SEO Training Institute in Chennai
search engine optimization course
SEO training in vadapalani
SEO training in porur
Python Training in Chennai
Hadoop Training in Chennai
Big data training in chennai
JAVA Training in Chennai

High Technologies Solutions said...

I really like your post. I found it very helpful. Please keep posting such kind of valuable post.
C++ Course in Delhi

andriya said...

Nice article, its very informative content..thanks for sharing...Waiting for the next update...
Drupal Training in Chennai
Drupal Course in Chennai
Drupal Training
Drupal Training in OMR
Drupal Training in Porur
Photoshop Classes in Chennai
clinical sas training in chennai
SAS Training in Chennai
javascript training in chennai
Hibernate Training in Chennai

Riya Raj said...

Good Blog!!! The way you have conveyed your blog is more impressive...
JAVA Training in Chennai
java class
best java coaching center in chennai
Java training institute in chennai
java training in Guindy
JAVA Training in Tambaram
Python Training in Chennai
Big data training in chennai
SEO training in chennai
Selenium Training in Chennai

Manisha Sudha said...

Awesome blog...thanks for sharing valuable articles.....
Struts Training in Chennai
Struts Training institutes in Chennai
Struts Training Chennai
struts Training in Anna Nagar
struts Training in T Nagar
Wordpress Training in Chennai
SAS Training in Chennai
Spring Training in Chennai
Photoshop Classes in Chennai
DOT NET Training in Chennai

Anjudevan said...

Awesome Article...Thanks for sharing...waiting for next update...
Wordpress Training in Chennai
Best Wordpress Training in Chennai
WordPress course
Wordpress Training in Velachery
Wordpress Training in Tambaram
Struts Training in Chennai
clinical sas training in chennai
Spring Training in Chennai
Photoshop Classes in Chennai
LoadRunner Training in Chennai

Prakash said...

More impressive blog!!! Thanks for shared with us.... waiting for you upcoming data.
Software Testing Training in Chennai
software testing course in chennai
testing courses in chennai
software testing training institute in chennai
Software testing training in Thiruvanmiyur
Software testing training in Velachery
Python Training in Chennai
Digital marketing course in chennai
Python Training in Chennai
JAVA Training in Chennai

chintu said...

valuable blog,Informative content...thanks for sharing, Waiting for the next update...
C C++ Training in Chennai
c c++ courses in chennai
best c c++ training in chennai
C C++ training in OMR
C C++ training in T Nagar
javascript training in chennai
core java training in chennai
Html5 Training in Chennai
DOT NET Training in Chennai
QTP Training in Chennai

vinudevan said...

Valuable one...thanks for sharing...
DOT NET Training in Chennai
Best DOT NET Training institute in Chennai
Best DOT NET Training in Chennai
dot net coaching centers in chennai
dot net training in Guindy
Html5 Training in Chennai
Spring Training in Chennai
Struts Training in Chennai
Wordpress Training in Chennai
SAS Training in Chennai

sridharraj said...

Awesome Blog...Thanks for sharing, Waiting for next update...
Hibernate Training in Chennai
Spring Hibernate Training in Chennai
Spring and Hibernate Training in Chennai
hibernate training in anna nagar
hibernate training in vadapalani
Spring Training in Chennai
clinical sas training in chennai
DOT NET Training in Chennai
QTP Training in Chennai
LoadRunner Training in Chennai

Prakash said...

Wonderful Blog.... Thanks for sharing with us...
Hadoop Training in Chennai
Big data training in chennai
Big Data Hadoop Training in Chennai
big data certification in chennai
big data Training in Tnagar
Hadoop training in Thiruvanmiyur
Software testing training in chennai
Android Training in Chennai
JAVA Training in Chennai
Selenium Training in Chennai

Mani Mekalai said...

Valuable one,waiting for next upate...thanks for sharing...
Html5 Training in Chennai
html course fees in chennai
Html5 Training
Html5 Training in Tambaram
Html5 Training in Anna Nagar
DOT NET Training in Chennai
core java training in chennai
Hibernate Training in Chennai
Mobile Testing Training in Chennai
SAS Training in Chennai

Prakash said...

Wonderful Post!!! Thanks for sharing this great blog with us.
Android Training in Chennai
Android Training Institute in Chennai
android training center in chennai
app development course in chennai
Android Training in Tnagar
Android training in Thiruvanmiyur
Big data training in chennai
Software testing training in chennai
Selenium Training in Chennai
JAVA Training in Chennai

Vivek Vichu said...


Nice Blog...Thanks for sharing the article waiting for next update...
Artificial Intelligence Course in Chennai
artificial intelligence training in chennai
Mobile Testing Training in Chennai
C C++ Training in Chennai
javascript training in chennai
Html5 Training in Chennai
QTP Training in Chennai
Spring Training in Chennai
DOT NET Training in Chennai

Pooja Patel said...

More impressive blog!!! Thanks for shared with us.... waiting for you upcoming data.
Software Testing Training in Chennai
software testing course in chennai
testing courses in chennai
best software testing training institute in chennai with placement
Software testing training in Adyar
Software testing training in Tnagar
Big data training in chennai
Hadoop training in chennai
Android training in Chennai
Selenium Training in Chennai

Laura Bush said...

I absolutely respect and acknowledge your point on each and every subject. Keep up the great work. if you are looking the Best C++ Course In Delhi, then contact us.

shemadevan said...

Thanks for giving excellent Message.Waiting for next article
LoadRunner Training in Chennai
Loadrunner course in Chennai
Best Loadrunner training institute in chennai
Loadrunner Training in Velachery
Loadrunner Training in Tambaram
QTP Training in Chennai
core java training in chennai
C C++ Training in Chennai
Mobile Testing Training in Chennai
Manual Testing Training in Chennai

Prakash said...

Awesome Post!!! Thanks for sharing this great post with us.
JAVA Training in Chennai
Best JAVA Training institute in Chennai
best java course in chennai
JAVA J2EE Training in Chennai
Best JAVA Training in Chennai
java training in OMR
JAVA Training in Annanagar
Big data training in chennai
Selenium Training in Chennai
Android Training in Chennai

harish kalyan said...

Really a awesome blog for the freshers. Thanks for posting the information.
German Classes in Chennai
German language training in chennai
french classes
pearson vue test center in chennai
IoT Training in Chennai
Xamarin Training in Chennai
Node JS Training in Chennai
content writing course in chennai
German Classes in Velachery
German Classes in Adyar

The India said...

khadi sarees online
khadi kurta online
khadi kurta for ladies
kurtas for salee
khadi clothes for yoga
khadi silk saree
khadi silk sarees
handloom khadi sarees