-
Notifications
You must be signed in to change notification settings - Fork 1
/
My_Number_Guessing_Game.c
36 lines (33 loc) · 1.14 KB
/
My_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
//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, nguesses = 1;
srand(time(0));
number = rand() % 1000 + 1; // It generates a number b/w the 1 to 1000
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 the number please!\n");
}
else
{
printf("You guessed it in %d attempts\n", nguesses);
}
nguesses++;
} while (guess != number);
return 0;
}