42.6k views
0 votes
Write a do-while loop that continues to prompt a user to enter a number less than 100, until the entered number is actually less than 100. End each prompt with a newline. Ex: For the user input 123, 395, 25, the expected output is:

Enter a number (<100):
Enter a number (<100):
Enter a number (<100):
Your number < 100 is: 25
c++

#include
using namespace std;

int main() {
int userInput = 0;

do
cout << "Your number < 100 is: " << userInput << endl;

return 0;
}

User Deejay
by
7.4k points

1 Answer

3 votes

Answer:

#include <iostream>//including iostream library to use functions such as cout and cin

using namespace std;

int main() {

int userInput = 0;

do

{

cout << "Enter a number < 100: " ;

cin >> userInput;

if (userInput < 100)//condition if number is less than 100

{

cout << "Your number < 100 is: " << userInput << endl;

}

} while (userInput > 100);//do while loop condition

return 0;

}

Step-by-step explanation:

A do-while loop executes regardless in the first iteration. Once it has run through the first iteration, it checks if the condition is being met. If, the condition is TRUE, the loop begins the second iteration. If FALSE, the loop exits. In this case, the condition is the userInput. after the first iteration, lets say the userInput is 120, the condition userInput > 100 is true.Therefore, the loop will run again until eventually the number is less than hundred, lets say 25. In that case the condition would be 25 > 100, which would be false, so the loops will break.

User Jorgy
by
8.0k points