Answer to Question #308437 in C++ for abdulahi

Question #308437

program that computes the sum and the difference and the product of a range of consecutive numbers where the starting and ending numbers of the range are to be entered from the keyboard


1
Expert's answer
2022-03-09T10:24:34-0500
#include <iostream>
using namespace std;


int main() {
    int start, end;


    cout << "Enter a start and an end of the region: ";
    cin >> start >> end;


    int sum=0, dif=0, prod=1;
    for (int i=start; i<=end; i++) {
        sum += i;
        if (i == start) {
            dif = start;
        }
        else {
            dif -= i;
        }
        prod *= i;
    }


    cout << "The sum is " << sum << endl;
    cout << "The difference is " << dif << endl;
    cout << "The product is " << prod << 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