Question: 2 . ( Using simple while loop with break statement ) Write a C program that generates a random number between 1 and 5 and
Using simple "while" loop with break statement
Write a C program that generates a random number between and and asks the user to guess it Use a while loop to give the user multiple chances until they guess the correct number.
#include
#include
#include
int main
srandtimeNULL; Seed the random number generator with the
current time
int targetNumber rand; Generate a random number
between and
int userGuess; Variable to store the user's guess
int numattempts ; Variable to count the number of attempts
printfGuess the number between and :
;
Pseudocode of your "while" loop:
Display "Input your guess:
Read the user's guess into userGuess
Increment attempts by
If userGuess is equal to targetNumber, then
Display "Congratulations! You guessed the correct number in numattempts attempts."
Exit the loop using "break" statement
Else, Display "Incorrect guess. Try again!"
Expected output:
Guess the number between and :
Input your guess:
Incorrect guess. Try again!
Input your guess:
Incorrect guess. Try again!
Input your guess:
Congratulations! You guessed the correct number in attempts
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
