-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber_Guessing_Game.c
70 lines (57 loc) · 2.07 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
// introducing user to game
printf("Welcome to Guess the number Game\n");
printf("I hope you like it\n");
// this variable contains the value of random number
int random_number;
// this variable contain the value of number input by user
int input_number;
// this variable store the value how many time user
// guess to find actual number
int guess_counter = 0;
// this helps us to get random number between 1 to 100
srand(time(0));
random_number = rand() % 100 + 1;
// taking input from user
printf("Guess the number (number is between 1 to 100): ");
scanf("%d", &input_number);
// creating loop
// the condition to run the loop is, if input_number is not equal to random_number
while (input_number != random_number)
{
// this if statement work when the Input number is greater than random number
if (input_number > random_number)
{
printf("Your guess is greater than actual number, Guess Smaller number\n");
}
// this if statement work when the Input number is smaller than random number
else if (input_number < random_number)
{
printf("Your guess is smaller than actual number, Guess Greater number\n");
}
// this code increase guess_counter by +1
guess_counter++;
// taking input from user again
printf("Guess the number again: ");
scanf("%d", &input_number);
}
// if the user guess the correct number this if
// statement runs
if (input_number == random_number)
{
printf("The number is %d\n", random_number);
printf("You guess the correct number after guessing %d times\n", guess_counter);
printf("Thanks for playing the game\n");
}
// if user not coperate or if somthing wrong happen this code runs
else
{
printf("Somthing error occur. Please, run the program again and follow the instructions\n");
printf("Sorry for error\n");
}
return 0;
}