Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Problem Description: Sum of two integers without using +/- operators.
- * Author: HTA
- * Date: July 13, 2016
- */
- #include <iostream>
- using namespace std;
- int add(int,int);
- int main(void) {
- int a,b;
- cout << "Give a: "; cin >> a;
- cout << endl << "Give b: "; cin >> b;
- cout << endl << "The Sum is: " << add(a,b) << endl;
- return 0;
- }
- int add(int x, int y) {
- if (y==0)
- return x;
- else {
- int sum= x^y;
- int carry= (x&y)<< 1;
- add(sum,carry);
- }
- }
- // alternative:
- /*
- *#include <iostream>
- *using namespace std;
- *
- *int main(void) {
- * int a,b;
- * cout << "Give a: "; cin >> a;
- * cout << endl << "Give b: "; cin >> b;
- * int sum,carry;
- * while (b!=0) {
- * sum= a^b;
- * carry= (a&b);
- * a=sum;
- * b=carry <<1;
- * }
- * cout << endl << "The Sum is: " << a << endl;
- * return 0;
- *}
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement