Answer to Question #152430 in C++ for Usama Saleem

Question #152430
Write a program which takes two integers as input and displays the result of subtracting the second number from the first. You are not allowed to use the "-" operator. You can only use a for loop and the increment and decrement operators.
1
Expert's answer
2020-12-25T15:16:46-0500
#include<stdio.h>
#include <iostream>
using namespace std;

int subtract(int x, int y)
{
    if (y == 0)
        return x;
    return subtract(x ^ y, (~x & y) << 1);
}

int main()
{
    int x, y;
    cout << "Enter two integers:";
    cin>>x>>y;
    printf("x - y is %d", subtract(x, y));
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog