Program: Guess the Number
You are tasked with creating a "Guess the Number" C program. The program will generate a random number between 0 and 20, and the user needs to guess this number within five tries. The program should provide feedback on whether the user's guess is too high or too low after each attempt.
Algorithm and Pseudocode
Generate a random number between 0 and 20.
Initialize a variable to count the number of tries, set it to 5.
Display a welcome message and the range of numbers (0 to 20).
Enter a loop that continues until the user guesses the correct number or runs out of tries. a. Prompt the user to enter a guess. b. Check if the guess is correct:
If yes, display a congratulatory message and end the loop.
If no, provide feedback on whether the guess is too high or too low. c. Decrement the number of tries. d. If the number of tries reaches zero, end the loop and display a message indicating the game is over.
Ways to Solve the Problem
Use a loop and conditional statements to compare the user's guess with the generated number.
Utilize a
do-while
loop to ensure the user gets at least one chance to guess.Implement error handling to validate that the user enters numbers within the specified range (0 to 20).
Utilize the
rand()
function to generate a random number.
Program
This C program implements the described "Guess the Number" game with error handling and informative messages for the user. It uses a do-while
loop to ensure that the user gets at least one chance to guess and provides feedback on whether the guess is too high or too low. The program also checks for invalid guesses and displays appropriate messages. If the user guesses correctly, a congratulatory message is displayed, and if they run out of tries, the game ends with the correct number revealed.