Home » Programming Tips & Tricks » C++ - Tips & Tricks

Find sum without using '+' operator in C++

By: IncludeHelp, on 16 FEB 2017

Learn: How to find sum/addition of two integer number without using plus (+) operator in C, C++ programming language by using just a simple mathematics trick?

Hey, this is a cool trick to find sum/addition of two integer numbers without using plus (+) operator in C and C++ programming languages?

Steps

  1. Take two integer numbers
  2. Assign (input) values to them
  3. Subtract second number from first number (after making first number negative)
  4. Finally make negative their result
sum=(-a-b);
sum=-sum;

OR

sum=-(-a-b);

Example:

Consider this example, here we are taking two integer variables a and b, assigning 10 and 20 to them and storing their addition in variable sum.

#include <iostream>
using namespace std;

int main()
{
	int a,b,sum;
	a=10;
	b=20;
	
	sum=-(-a-b);
	
	cout<<"sum is: "<<sum<<endl;
	
	return 0;
}

Output

sum is: 30





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.