Hello, please check this. This program is a guessing game that counts how many t
ID: 3774734 • Letter: H
Question
Hello, please check this.
This program is a guessing game that counts how many tries it takes for a player to guess a random number in the range from 1 to 20 (inclusive) that the computer has generated. The program should prompt the user for a guess and then indicate if the guess was too high, too low, or correct. When the player guesses correctly, the program should print out how many guesses the player made.
Here is my answer:
#include <stdio.h>
int main()
int random = (rand() % 20) + 1;
int guess = 0, numGuess = 0;
while(guess != random)
{
printf( "What is your guess?");
scanf( "guess");
if(guess < random)
scanf( "Your guess was too low, guess again.");
else if(guess > random)
printf( "Your guess was too high, guess again.");
return 0;
}
Explanation / Answer
#include <stdio.h>
int main()
{
int random = (rand() % 20) + 1;
int guess = 0, numGuess = 0;
while(guess != random)
{
printf( "What is your guess?");
scanf("%d",&guess);
if(guess < random)
scanf( "Your guess was too low, guess again.");
else if(guess > random)
printf( "Your guess was too high, guess again.");
numGuess++;
}
printf("you guessed in %d chances",numGuess);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.