10,060 views
37 votes
37 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 Bas In Het Veld
by
2.5k points

1 Answer

15 votes
15 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 Fulvio Flaco
by
2.7k points