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

Question #152427
Write a program which takes as input two integers and displays the sum of all the integers that lie between those two numbers. E.g., if x is 4 and y is 8, the answer would be 18 and if x is 8 and y is 4, the answer would be 18.
1
Expert's answer
2020-12-24T13:54:48-0500
#include <iostream>
using namespace std;
int main() {
    while (true) {
        cout << "=====stop the cycle please enter 0 for first number====\n";
        int a, b, Sum = 0;
        cout << "Enter the first number: "; cin >> a;
        cout << "Enter the second number: "; cin >> b;
        if (a == 0 or b == 0) {
            break;
        }
        if (a < b) {
            for (int i = a + 1; i < b; i++) {
                Sum += i;
            }
            cout << "The sum of numbers between [" << a << ", " << b << "] is " << Sum << endl;
        }
        else if (a > b) {
            for (int i = b + 1; i < a; i++) {
                Sum += i;
            }
            cout << "The sum of numbers between [" << b << ", " << a << "] is " << Sum << endl;
        }
        else {
            cout << "The numbers are equal" << endl;
        }
    }
    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