By SHUBHAM GARG
This article is about adding the two numbers without using arithmetic operators like +, - and * implemented in C++.
Hey Coders!
We are familiar with adding the two numbers using arithmetic operator + but in this article we have some interesting method to add the two numbers.
Let's see:
// Program to add two numbers without using arithmetic operators. #include using namespace std; // Main Function int main() { int num1, num2, carry, a, b; cout<<"Enter the 1st Number: "; cin>>a; cout<<"Enter the 2nd Number: "; cin>>b; while(b!=0) { //carry containing common set bits in a and b carry = a & b; // and operator //sum of bits of a and b only where at least one of the bits in them is not set a = a ^ b; // xor operator //carry gets shifted by one so that after adding it to a gives the required sum b = carry << 1; } cout<<"Sum is: "<<a; return 0; }
Happy Learning!
Submitted by SHUBHAM GARG (Shubham)
Download packets of source code on Coders Packet
Comments