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





Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.



Comments and Discussions


We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.