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!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.