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





85 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

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

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

nice one

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 .

lokesh said...

This blog is very helpful for us...I got some important information from this blog..
DOT NET Training in Chennai
DOT NET Training in Bangalore
asp .net training in chennai
dot net institute in bangalore
aws training in bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in bangalore
spoken english classes in bangalore
dot net training in marathahalli

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

Vivek Vichu said...

Awesome blog, very informative content... Thanks for sharing waiting for next update...
Artificial Intelligence Course in Chennai
AI Training in chennai
best artificial intelligence 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

Unknown 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

Unknown 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

Sreyas Ram said...

nice blog..valuable information....thanks for sharing...
Study Abroad Consultants in Kerala
study abroad consultants in thrissur
Study Abroad Consultants in Calicut
abroad job consultancy in coimbatore
best overseas education consultants in thrissur
overseas education consultants in kozhikode

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

Jayalakshmi said...

Thanks a lot for sharing such a good source with all, i appreciate your efforts taken for the same. I found this worth sharing and must share this with all.


Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery


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

praveen said...

Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me.





Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

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 Course said...

After reading your article I was amazed. I know that you explain it very well. And I hope that other readers will also experience how I feel after reading your article.
Ethical Hacking Course in Bangalore
Certified Ethical Hacker Course

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

Ethical Hacking in Bangalore said...

I am impressed by the information that you have on this blog. Thanks for Sharing
Ethical Hacking in Bangalore
Certified Ethical Hacker Course

Cyber Security said...

Thumbs up guys your doing a really good job. It is the intent to provide valuable information and best practices, including an understanding of the regulatory process.
Cyber Security Course in Bangalore

Cyber Security Course said...

Very nice blog and articles. I am really very happy to visit your blog. Now I am found which I actually want. I check your blog everyday and try to learn something from your blog. Thank you and waiting for your new post.
Cyber Security Training in Bangalore

Cyber Security Training said...

I will really appreciate the writer's choice for choosing this excellent article appropriate to my matter. Here is deep description about the article matter which helped me more.
Best Institute for Cyber Security in Bangalore

mary Brown said...

IEEE Final Year projects Project Centers in India are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. A IEEE Domain project Final Year Projects for CSE system development life cycle is essentially a phased project model that defines the organizational constraints of a large-scale systems project.

IT Company Employess Productivity usually increases when a company implements corporate training courses on latest technologies.
corporate training in chennai
It Companies need of Corporate training programme arises due to improvement in technology, need for getting better performance or as part of professional development. corporate training companies in chennai Corporate Training refers to a system of professional development activities provided to educate employees.
corporate training companies in india

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

lionelmessi said...

Really Very Infromative Post , Thanks For Sharing The Information With Us.
Best AWS Training Institute in Hyderabad

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 Training in Bangalore said...

Very useful message. This is my first time visiting here. I found a lot of interesting things on your blog, especially your discussion. It really is a great article. Keep on going.

360DigiTMG Data Science Courses

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

Sages Marketing said...

Very interesting blog. Many blogs I see these days do not really provide anything that attracts others, but believe me the way you interact is literally awesome.You can also check my articles as well.

Security Guard License
Ontario Security License
Security License Ontario
Security License

Thank you..

Tech Institute said...

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

Business Analytics Course said...

He's really nice and mean. it's a really cool blog. The link is a very useful thing. You have really helped a lot of people who visit the blog and give them useful information.

Business Analytics Course in Bangalore

Data Analytics Course said...

I always search online for articles that can help me. Obviously, there is a lot to know about this. I think you made a few good points about the features as well. Keep up the good work!

Data Analytics Course in Bangalore

Pankaj Singh 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

Artificial Intelligence Course said...

Now is the perfect time to plan for the future and now is the time to be happy. I have read this article and if I can I would like to suggest some cool tips or advice. Perhaps you could write future articles that reference this article. I want to know more!

Artificial Intelligence Course in Bangalore

Mike Johnson said...

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

Unknown said...

Water bodies are the main source of transportation for international freight forwarding. Due to this, sea freight company in Delhi,
visit
Freight Forwarder in Vietnam
Shipping Company In India

Unknown said...

visit here
iso certification in delhi
iso certification in noida
iso certification in gurgaon
iso certification in faridabad

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

Shayari Night said...

Webocity is best website designing company in delhi , Best Website development company in Delhi, We Offer Best 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

Trending Technologies said...

Fantastic article and excellent topic with valuable information thanks for sharing.
Data Science Course in Bangalore

PMP Certification said...

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

Priyanka said...

Attend The Data Analyst Course From ExcelR. Practical Data Analyst Course Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Data Analyst Course.
Data Analyst Course

Banumadhu said...

Excellent blog thanks for sharing the valuable information..it becomes easy to read and easily understand the information.
Useful article which was very helpful. also interesting and contains good information.
to know about python training course , use the below link.

Python Training in chennai

Python Course in chennai




DataScience Specialist said...

I have to search sites with relevant information ,This is a
wonderful blog,These type of blog keeps the users interest in
the website, i am impressed. thank you.
Data Science Course in Bangalore

Artificial Intelligence Course 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.

Artificial Intelligence Course in Bangalore

Data Science Training in Bangalore 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

Mallela said...

Thanks for posting the best information and the blog is very informative.Data science course in Faridabad

Srigokul said...


Nice Blog. Thanks for Sharing this useful information...

Data science training in chennai
Data science course in chennai

Data Science Training in Chennai said...

I am glad to discover this page. I have to thank you for the time I spent on this especially great reading !! I really liked each part and also bookmarked you for new information on your site.
Data Science Training in Chennai

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

Bali Digital Enthusiast said...

Evo Syah adalah seorang Freelancer Website Developer Bali, dia sangat menyukai hobbynya dalam menelusuri setiap blok yang Anda di internet. Dia memiliki banyak pengetahuan tentang Digital Marketing dan bagaimana penerapan dalam bisnis.

Bali Digital Enthusiast said...

Satya Puri Studio is Bali architects have worked on historically-inspired homes & villa around the world — including “Lux Villa in Bali; Sukh Sagar Haveli in Jodhpur, Rajasthan; Kahani Paradise in Gokarna. Their work shows inspiration across history and styles, tied together with the goal of enhancing life through good design. Above all, Satya Puri Studio Architects aims to produce personalized living environments.

Bali Digital Enthusiast said...

SEO adalah proses meningkatkan visibilitas dan peringkat website bisnis Anda di laman hasil penelusuran mesin telusur, seperti Google, Bing, dan Yahoo, sehingga memudahkan pelanggan menemukan produk atau layanan Anda secara online.

Kami menawarkan layanan Jasa SEO terbaik di Bali dengan Strategi Optimasi Situs Website secara tepat dan akurat sesuai dengan prinsip kerja mesin pencari.

Bali Digital Enthusiast said...

When you think Indonesia, you probably picture its Labuan Bajo, and sunny beaches in Canggu. Yet, many people don’t realize that Indonesia is also a great place to start a business. According to International Labour Organization, there are about 700 thousand small businesses in Indonesia in 2018, which 57 million are small and medium sized enterprises Start Business in Indonesia

But what makes Indonesia so great? Major cities like Jakarta, Bandung, Yogyakarta, and Surabaya, an impressive university system, and low taxes means the Sunshine country is ripe with opportunity for savvy business owners. Keep reading to learn more about why Indonesia could be the perfect spot to start your new business!

Tech said...

I really enjoy every part and have bookmarked you to see the new things you post. Well done for this excellent article. Please keep this work of the same quality.
Artificial Intelligence course in Chennai

Deekshitha said...

Informative blog
Data Science Course in India

Mallela said...

Thanks for posting the best information and the blog is very helpful.Data science course in Varanasi

Mallela said...

Thanks for posting the best information and the blog is very helpful.artificial intelligence course in hyderabad

Deekshitha said...

Informative blog
ai training in hyderabad

Pallavi reddy said...

i am glad to discover this page : i have to thank you for the time i spent on this especially great reading !! i really liked each part and also bookmarked you for new information on your site.
best data science courses in bangalore