Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Cpp program to sum or sub two integers (without +/- operators)
- * Author: Taqui ; Date : 14 July,2016
- * ###Copyright: All Rights Reserved###
- */
- #include <iostream>
- using namespace std;
- // prototype declarations for sum and sub functions.
- int summation(int,int);
- int subtraction(int,int);
- // main function starts:-
- int main(void) {
- int a,b;
- cout << "Here a and b are two integers." << endl;
- cout << "Now,\ngive a: "; cin >> a;
- cout << "And,\ngive b: "; cin >> b;
- cout << "\nNow, Summation(a+b) of Subtraction(a-b)? Type (Sum/Sub): "; string choice; cin >> choice;
- if (choice=="Sum")
- cout << "\nThe summation(a+b) is: " << summation(a,b) << endl;
- else if (choice=="Sub")
- cout << "\nThe subtraction(a-b) is: " << subtraction(a,b) << endl;
- cout << "\nRe-run the program? Type (Yes/No): " << endl; cin >> choice;
- if (choice=="Yes")
- { cout << endl; main(); }
- else
- cout << "\nThank You." << endl;
- return 0;
- }
- // definitions of several declared functions:-
- int summation (int x,int y) {
- if (y==0)
- return x;
- int carry= (x&y); // and operation
- x= x^y; // xor operation
- y= carry <<1; // left shift operator
- summation(x,y); // recursion
- }
- int subtraction (int x,int y) {
- if (y==0)
- return x;
- int borrow= (~x)&y;
- x= x^y;
- y= borrow <<1;
- subtraction(x,y);
- }
Advertisement
Add Comment
Please, Sign In to add comment