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,
#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;
}
Comments
Leave a comment