Do-while loops

Basic do-while loop with user input.

Complete the do-while loop to output 0 to countLimit using printVal. Assume the user will only input a positive number.

#include <stdio.h>

int main(void) {
 int countLimit;
 int printVal;

// Get user input
 scanf("%d", &countLimit);

printVal = 0;
 do {
 printf("%d ", printVal);
 printVal = printVal + 1;
 } while ( printVal <= countLimit );
 printf("\n");

return 0;
}

 

Do-while loop to prompt user input.

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 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

solution

#include <stdio.h>

int main(void) {
 int userInput;

do {
 printf("Enter a number (<100):");
 printf("\n");
 scanf("%d", &userInput);
 }
while(userInput>= 100);

printf("Your number < 100 is: %d\n", userInput);

return 0;
}