In: Computer Science
C Program and pseudocode for this problem.
Write a C program that plays the game of "Guess the number" as the following: Your program choose the number to be guessed by selecting an integer at random in the rang of 1 to 1000. The program then asks the use to guess the number. If the player's guess is incorrect, your program should loop until the player finally gets the number right. Your program keeps telling the player "Too High" or "Too Low" to help the player "zero in" on the correct number.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(0));
int num=(rand()%1000)+1;// To generate numbers in the range of 1 to 1000.
int guess;
scanf("%d",&guess);//taking input guess from the user
//printf("%d\n",num); //you can uncomment this to check the random number
//until the guess is incorrect it will take input from the user
while(num!=guess)
{
if(num>guess)//if the guess is lower than the number it will output Too Low
{
printf("Too Low\n");
}
else//if the guess is higher than the number it will output Too High
{
printf("Too High\n");
}
scanf("%d",&guess);
}
printf("zero in");//it will output zero in when user guesses the number right
return 0;
}
Pseudocode code
Declare num
store random number in num
Declare guess
Take input from user and store it in guess
Run a loop until the guess is equal to num
if guess is higher than num then
print "Too High"
else
print " Too low"
again take input from user and check the conditions
if guess is equal to num then
Print "Zero in"