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





62 comments:

Unknown said...

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

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

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

Unknown 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!!!

Unknown 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 Singh said...

Good thing boss

Unknown 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.

Unknown 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

Unknown 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

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

Unknown said...

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

Krishna said...

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

sasi said...

The blog you shared is very good. I expect more information from you like this blog. Thankyou.
Web Designing Course in chennai
Web Designing Course in bangalore
web designing course in coimbatore
web designing training in bangalore
web designing course in madurai
Web Development courses in bangalore
Web development training in bangalore
Salesforce training in bangalore
Web Designing Course in bangalore with placement
web designing training institute in chennai

Big Data Analytics Malaysia said...

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.




artificial intelligence in malaysia

yadav said...

Nice Blog. Keep update more information about this..
IELTS Coaching in Chennai
IELTS coaching in bangalore
IELTS coaching centre in coimbatore
IELTS coaching in madurai
IELTS Coaching in Hyderabad
Best ielts coaching in bangalore
ielts training in bangalore
ielts coaching centre in bangalore
ielts classes in bangalore
ethical hacking course in bangalore

Anonymous said...

Excellent Post, Interesting Article.Indias Fastest Local Search Engine
Salesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune

Anonymous said...

Thanks for your efforts in sharing the knowledge to needed ones. Waiting for more updates. Keep continuing.
Salesforce Training | Online Course | Certification in chennai | Salesforce Training | Online Course | Certification in bangalore | Salesforce Training | Online Course | Certification in hyderabad | Salesforce Training | Online Course | Certification in pune

subha said...

plz also share the problem for multiplication with out using * operator!!! it may help us a lot thank u guys.
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

subha said...

Nice post. Thanks for sharing! I want people to know just how good this information is in your blog. It’s interesting content and Great work. eave some more info
Ai & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai

bairav said...

Awesome post. I am a normal visitor of your blog and appreciate you taking the time to maintain the excellent site. I’ll be a frequent visitor for a long time.

German Classes in Chennai | Certification | Language Learning Online Courses | GRE Coaching Classes in Chennai | Certification | Language Learning Online Courses | TOEFL Coaching in Chennai | Certification | Language Learning Online Courses | Spoken English Classes in Chennai | Certification | Communication Skills Training

Ethical Hacking Training said...

Wow! Such an amazing and helpful post this is. I really really love it. I hope that you continue to do your work like this in the future also.
Ethical Hacking Training in Bangalore
Ethical Hacking Training

Aishu said...

I appreciate that you produced this wonderful article to help us get more knowledge about this topic.
I know, it is not an easy task to write such a big article in one day, I've tried that and I've failed. But, here you are, trying the big task and finishing it off and getting good comments.
IELTS Coaching in chennai

German Classes in Chennai

GRE Coaching Classes in Chennai

TOEFL Coaching in Chennai

spoken english classes in chennai | Communication training

sowmi said...

astrologers in india
astrology online
best astrologer in andhra pradesh
best astrology online
astrology
famous astrologer in andhra pradesh
best astrologer near me
top 10 astrologers in andhra pradesh

Data Science Certification said...

I am here for the first time. I found this table and found it really useful and it helped me a lot. I hope to present something again and help others as you have helped me.

360DigiTMG Data Science Certification

Tech Institute said...

Great article with excellent information thank you.
Data Analytics Course Online 360DigiTMG

OGEN Infosystem (P) Limited said...

This blog was really good formatting, thanks for sharing with us. Visit Ogen Infosystem for professional website designing and digital marketing services at good price.
Website Designing Company in Delhi

Mike Johnson said...

I usually buy youtube views for my youtube video from this site https://soclikes.com/. Maybe you will do the same?

Mike Johnson said...

If you start youtube blog, I advise you to buy youtube subscribers from this site https://viplikes.in

Online Front said...

Such a great and unique information, really glad to learn something new from you. Good job and keep it up.

website development packages
SMM service
web development packages
1000 free youtube subscribers
Digital Marketing Services in delhi

Huongkv said...

Aivivu - chuyên vé máy bay, tham khảo

vé máy bay đi Mỹ khứ hồi

vé máy bay tết 2021 pacific airlines

giá vé máy bay đi toronto Canada

săn vé máy bay đi Pháp

ve may bay di Anh

cách săn vé máy bay giá rẻ

combo đi đà lạt

combo nha trang tháng 8

Bhavana said...

Pleasant article, I acknowledged examining your post, fair offer, I need to joke this to my disciples. Much valued!.
AI Course

Professional Course said...

Really, this article is truly one of the best in the article history. I am a collector of old "items" and sometimes read new items if I find them interesting. And this one that I found quite fascinating and should be part of my collection. Very good work!

Business Analytics Course

Huongkv said...

Aivivu chuyên vé máy bay, tham khảo

vé máy bay đi Mỹ giá rẻ

vé từ mỹ về việt nam

vé máy bay 2 chiều hà nội đà nẵng

giá vé máy bay hà nội đà lạt khứ hồi

giá vé máy bay sài gòn phú quốc

AI Courses said...
This comment has been removed by the author.
360DigiTMG-Pune said...

This is a fantastic website , thanks for sharing.
artificial intelligence course in pune

Digital Chandu said...

Thanks For Your Post, Are You Interested to learn digital marketing training for free, here is a source for you.

digital marketing training institute
digital marketing course Training in madhapur
digital marketing Training near me
digital marketing training in vijaywada
digital marketing training in hyderabad

Ramesh Sampangi said...

Thanks for sharing this blog. I really appreciate your work on this blog.
Artificial Intelligence Course
Data Science Training

kumal kumar said...

Really nice and interesting post. I was looking for this kind of information, enjoyed reading this one thank you.
Artificial Intelligence Companies in Bangalore

토토사이트 said...

As I am looking at your writing, 온카지노 I regret being unable to do outdoor activities due to Corona 19, and I miss my old daily life. If you also miss the daily life of those days, would you please visit my site once? My site is a site where I post about photos and daily life when I was free.


Emma King said...

One of the primary keys to web design for eCommerce is usability. An eCommerce website design company in Delhi must allow simple and practical navigation to its users both in PC and mobile versions.
Website Development Company In Dubai
Website Development Company In Australia

Sruthi Karan said...

It was really awesome and I gain more information from your post. Thank you!
Fairfax Divorce Lawyers

Sruthi Karan said...

Excellent blog!!! I really enjoy to read your post and thanks for sharing!
Online Solicitation Of A Minor
Abogado De Divorcio En Virginia

Molly Greville said...

Are you looking for a great way to Learn To Sing For Beginners in sydney, Australia? We offer affordable singing lessons for beginners that will help you develop your vocal skills and confidence. Contact us today to book your first lesson!

Ambika Kuntal said...

Are you looking for a Search Property Buyers Agent in Brisbane, Australia? Asset Plus Buyers Agents can help you find the perfect property. We have many listings, from houses to apartments and flats. We can also help you find the right financing and mortgage options. Contact us today to learn more!

Venkat Kaur said...

Are you looking for a great way to keep your home cool in the summer and warm in the winter? Outdoor Roller Blinds in Melbourne, Australia are a great solution! Divine Interiors offers a wide selection of high-quality roller blinds that are perfect for any home in Melbourne.

Rupesh Kumar said...

This post is so helpfull and attractive.keep updating with more information. Ziyyara Edutech’s comprehensive online AS Level courses. Designed specifically for AS Level students.
Book A Free Demo Today visit online AS level course

https://apkmodguru.com/ said...

your article is greatfull and thanks for sharing this article in my family. your article is very happy more aticle is share.Operation Valentine Movie (2024)

BCL said...

Nice post. Thanks for sharing. Best Digital Marketing Institute in Bangalore

kosmik said...

Nice Blog. Keep update more information about this.
java-full-stack-developer-training-in-hyderabad