write a c program that plays a guessing game with the user a
write a c program that plays a guessing game with the user. a sample run for the game as follows. user input is in boldface in the sample run.
welcome to the game of guess it!
i will choose a number between 1 and 100. you will try to guess that number.
if you guess wrong, i will tell you if you guessed too high or too low.
you have 6 tries to get the number.
ok, i am thinking of a number. try to guess it.
your guess? 50
too high
your guess?12
too low!
your guess? 112
illegal guess. your guess must be between 1 and 100. try again.
your guess?23
correct
want to play again? y
ok, i am thing of a number try to guess it .
your guess?23
correct
want to play again ? n
goodbye, it was fun. play again soon.
Solution
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
char ch;
int my_guess,user_guess = 0;
int guess = 0;
printf(\" welcome to the game of guess it! i will choose a number between 1 and 100. you will try to guess that number. if you guess wrong, i will tell you if you guessed too high or too low. you have 6 tries to get the number.\ \");
do{
printf(\"ok, i am thinking of a number. try to guess it.\ \");
my_guess = rand()%100+1;
guess = 0;
while(guess!=6)
{
printf(\"Your Guess? : \");
scanf(\"%d\",&user_guess);
if(user_guess>my_guess)
printf(\"too high.\ \");
else if(user_guess<my_guess)
printf(\"too low.\ \");
else if(user_guess<1 || user_guess>100)
printf(\"illegal guess. your guess must be between 1 and 100. try again.\ \");
else
{
printf(\"Correct.\ \");
break;
}
guess++;
}
printf(\"Want to play again?(y/n) : \");
scanf(\"%c\",&ch);
}while(ch != \'n\');
printf(\"goodbye, it was fun. play again soon.\ \");
return 0;
}

