We will write a program that generates a random number and asks the player to guess it. If the player’s guess is higher than the actual number, the program displays “Lower number please”. Similarly, if the user’s guess is too low, the program prints “Higher number please”. When the user guesses the correct number, the program displays the number of guesses the player used to arrive at the number.
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int number,guess,nguess=0;
srand(time(0));
number=rand()%100+1;
printf("The number is:%d",number);
for (int i=1;i;i++)
{
printf("Enter your guessed number between 1 to 100:");
scanf("%d",guess);
nguess=nguess+1;
if (number > guess)
{
printf("Higher number please!");
}
else if (number < guess)
{
printf("Lower number please!");
}
else
{
printf("You guessed it correctly in %d attempts",nguess);
break;
}
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire