forked from Sanyam-7/Hacktoberfest2022-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guess_game.c
41 lines (34 loc) · 1.22 KB
/
Guess_game.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*This is going to be fun!! 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.
Hints:
Use loops
Use a random number generator.*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number,guess,nguess=1;
srand(time(0));
number=rand()%100 + 1;// Generates a random number between 1 and 100
// printf("The number is %d\n", number);
// Keep running the loop until the number is guessed
do
{
printf("Guess the number between 1 to 100\n");
scanf("%d",&guess);
if (guess>number)
{
printf("Lower number please!");
}
else if (guess<number)
{
printf("Higher number please!");
}
else{
printf("You guessed it in %d attempts!",nguess);
}
nguess++;
} while (guess!=number);
return 0;
}