-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber-Guessing-Game.c
47 lines (37 loc) · 1.31 KB
/
Number-Guessing-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
42
43
44
45
46
47
/*
Project :- : Number guessing Game
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.
Hint :
Use loops
Use a random number generator
*/
#include <stdio.h>
#include <stdlib.h> // This library is used for generates random number
#include <time.h>
int main()
{
int number, guess, nguesses = 1;
srand(time(0)); // time(0) --> it returns time in seconds
number = rand() % 100 + 1; // Generates a random number between 1 and 100
// 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!\n");
}
else if (guess < number)
{
printf("Higher number please!\n");
}
else
{
printf("You guessed it in %d attempts\n", nguesses);
}
nguesses++;
} while (guess != number);
return 0;
}