0% found this document useful (0 votes)
2 views2 pages

C++ Program for Loop Selection

The document provides a C++ program that allows users to choose between three types of looping statements: while loop, do-while loop, and for loop. Users can input a starting and ending number, and the program will display the numbers in the specified range using the selected looping statement. The program includes error handling for incorrect choices.

Uploaded by

jhonbenedick2010
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

C++ Program for Loop Selection

The document provides a C++ program that allows users to choose between three types of looping statements: while loop, do-while loop, and for loop. Users can input a starting and ending number, and the program will display the numbers in the specified range using the selected looping statement. The program includes error handling for incorrect choices.

Uploaded by

jhonbenedick2010
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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,

Sample Answer:
#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;
}

You might also like