21.7k views
0 votes
Write a C++ program that enables users to choose the looping statement they want to use by combining conditional statements and looping statements. The initiative will

Allow users to specify the starting and ending numbers that will be displayed using the looping statement they have chosen.

User Off
by
7.9k points

1 Answer

4 votes

Answer:

#include <iostream>

using namespace std;

int main()

{

int start, end, choice;

cout << "Enter the starting number: ";

cin >> start;

cout << "Enter the ending number: ";

cin >> end;

cout << "Choose a looping statement: " << endl;

cout << "1. for loop" << endl;

cout << "2. while loop" << endl;

cout << "3. do-while loop" << endl;

cin >> choice;

if (choice == 1)

{

for (int i = start; i <= end; i++)

{

cout << i << endl;

}

}

else if (choice == 2)

{

int i = start;

while (i <= end)

{

cout << i << endl;

i++;

}

}

else if (choice == 3)

{

int i = start;

do

{

cout << i << endl;

i++;

} while (i <= end);

}

else

{

cout << "Invalid choice" << endl;

}

return 0;

}

User Pankaj Mahato
by
6.9k points