In: Computer Science
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly.
Rank the user as follows:
The program must include a while loop, if statements, and the following user-defined functions:
#include <stdio.h>
#include <stdlib.h>
int getRandomNumber();
int check4Win(int, int);
void printResults(int);
int main(){
int random,guess, counter = 0, result;
random = getRandomNumber();
while(1){
scanf("%d",&guess);
counter++;
result = check4Win(random,
guess);
if(result){
printf("correct");
printResults(counter);
break;
}else{
if(guess<random)
printf("too low\n");
else
if(guess>random)
printf("too high\n");
}
}
}
int getRandomNumber(){
return (1 + (rand() % 100));
}
int check4Win(int random, int guess){
if(random == guess)
return 1;
else
return 0;
}
void printResults(int counter){
if(counter>=1 && counter<=4)
printf("\nSuper Guesser");
else if(counter>=5 && counter<=6)
printf("\nExcellent
Guesser");
else if(counter>=7 && counter<=10)
printf("\nGood Guesser");
else if(counter>10)
printf("\nNovice Guesser");
}
Screenshot of the program
Output
NOTE: If you want to change something , please let me know through comments; I will surely revert back to you.
Please give a up vote .....
Thank you...