Write a code in c++ to print sum of two numbers without using arthematic operator | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Write a code in c++ to print sum of two numbers without using arthematic operator

29th Oct 2017, 3:30 AM
Shashank Keerthi
Shashank Keerthi - avatar
3 Answers
+ 2
@Nandu using ++ and -- counts as using arithmetic operators.
29th Oct 2017, 4:47 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
You could also use recursion: int add(int a, int b) { if(b==0) return a; else return add(a ^ b, (a & b) << 1); }
29th Oct 2017, 4:50 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
#include <iostream> using namespace std; int add(int a, int b) { while(b != 0) { int c = a & b; a = a ^ b; b = c << 1; } return a; } int main() { int a = 3, b = 5; cout << add(a, b); return 0; }
29th Oct 2017, 4:30 AM
ChaoticDawg
ChaoticDawg - avatar