Answer to Question #152768 in C++ for Noman khan

Question #152768
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-24T18:59:42-0500
#include <iostream>
using namespace std;
int main () {
  int a, b;
  cout << "Enter the first number: "; cin >> a;
  cout << "Enter the second number: "; cin >> b;
  int substraction = 0;
  if (a <= b) {
    for (int i = a; i < b; i++) {
      substraction++;
    }
  }
  else {
    for (int i = a; i > b; i--) {
      substraction--;
    }
  }
  cout << "second number - first number = " << substraction;
  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