write a C program that plays a number guessing game with the

write a C program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in [ ] in the sample run. (only if else, for while loops and int())

    

     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 thinking of a number. Try to guess it.

     Your guess? [23]

     ****CORRECT****

     Want to play again? [n]

     Goodbye, it was fun. Play again soon.

Solution

Guess.c


#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int randNum ;
int guess;
char ch;
int count = 0;
printf(\"Welcome to the game of Guess It!\ \");
printf(\"I will choose a number between 1 and 100.\ \");
printf(\"You will try to guess that number. If you guess wrong,\ \");
printf(\"I will tell you if you guessed too high or too low.\ \");
printf(\"You have 6 tries to get the number.\ \");
while(1){
printf(\"OK, I am thinking of a number. Try to guess it.\ \");
srand (time(NULL));
randNum = rand() % 100 + 1;
while(1){

printf(\"Your guess? \");
scanf(\"%d\", &guess);
if(guess < 1 && guess > 100){
printf(\"Illegal guess. Your guess must be between 1 and 100. Try again. \ \");
}
else if(randNum == guess){
printf(\"****CORRECT****\ \");
break;
}
else if(randNum < guess){
printf(\"Too high!\ \");
}
else if(randNum > guess){
printf(\"Too low!\ \");
}
count++;
if(count > 6){
printf(\"Sorry! you have exceeded max limit 6 guesses.\ \");
break;
}
}
printf(\"Want to play again? \");
scanf(\" %c\", &ch);
if(ch == \'n\' || ch == \'N\'){
printf(\"Goodbye, it was fun. Play again soon.\ \");
break;
}
count = 0;
}
return 0;
}

Output:

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

sh-4.3$ maincc -o main *.c                                                                                                                                                                                                  

sh: maincc: command not found                                                                                                                                                                                               

sh-4.3$ main                                                                                                                                                                                                                

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

Too high!                                                                                                                                                                                                                   

Your guess? 44                                                                                                                                                                                                              

Too low!                                                                                                                                                                                                                    

Your guess? 50                                                                                                                                                                                                              

Too low!                                                                                                                                                                                                                    

Your guess? 53                                                                                                                                                                                                              

Too high!                                                                                                                                                                                                                   

Your guess? 51                                                                                                                                                                                                              

****CORRECT****                                                                                                                                                                                                             

Want to play again? y                                                                                                                                                                                                       

OK, I am thinking of a number. Try to guess it.                                                                                                                                                                             

Your guess? 55                                                                                                                                                                                                              

Too high!                                                                                                                                                                                                                   

Your guess? 44                                                                                                                                                                                                              

Too high!                                                                                                                                                                                                                   

Your guess? 33                                                                                                                                                                                                              

Too high!                                                                                                                                                                                                                   

Your guess? 22                                                                                                                                                                                                              

Too high!                                                                                                                                                                                                                   

Your guess? 11                                                                                                                                                                                                              

Too low!                                                                                                                                                                                                                    

Your guess? 20                                                                                                                                                                                                              

Too high!                                                                                                                                                                                                                   

Your guess? 18                                                                                                                                                                                                              

Too high!                                                                                                                                                                                                                   

Sorry! you have exceeded max limit 6 guesses.                                                                                                                                                                               

Want to play again? n                                                                                                                                                                                                       

Goodbye, it was fun. Play again soon.

write a C program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in [ ] in the sample run. (only if els
write a C program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in [ ] in the sample run. (only if els
write a C program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in [ ] in the sample run. (only if els
write a C program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in [ ] in the sample run. (only if els

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site