Answer to Question #289130 in C++ for Josh Matusenos

Question #289130

Using conditional statement and looping statements, write a C++ program that will allow the users to select a looping statement which he/she wants to use. The program will also allow the users to enter the starting and ending number to display using the selected looping statement.


Sample Output:


Looping Statements:

1 - while loop

2 - do-while loop

3 - for loop


Enter the number of looping statement: 2


Enter Starting Number: 2

Enter Ending Number: 10


The Numbers: 2,3,4,5,6,7,8,9,10,


1
Expert's answer
2022-01-20T08:27:59-0500
#include <iostream>
using namespace std;

int main() {
    int start, end, i;
    int choice;

    cout << "Looping Statements:" << endl;
    cout << "1 - while loop" << endl;
    cout << "2 - do-while loop" << endl;
    cout << "3 - for loop" << endl;
    cout << endl << "Enter the number of looping statement: ";
    cin >> choice;

    cout << endl << "Enter Starting Number: ";
    cin >> start;
    cout <<"Enter Ending Number: ";
    cin >> end;

    if (choice == 1) {
        i = start;
        while (i <= end) {
            cout << i << ",";
            i++;
        }
    }
    else if (choice == 2) {
        i = start;
        do {
            cout << i << ",";
            i++;
        } while (i <= end);
    }
    else if (choice == 3) {
        for  (i=start; i<=end; i++) {
            cout << i << ",";
        }
    }
    else {
        cout << "Incorrect choice";
    }

    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