forked from Sniper7sumit/Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuess_The_Number.c
53 lines (48 loc) · 1.53 KB
/
Guess_The_Number.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
printf("\n\n");
printf("Hi! Welcome to THE GUESSING GAME!!\n");
printf("In this game, you have to guess a number between 1 and 50\n");
printf("You will get only 5 attempts to guess the number right\n\n");
int num, guess, c = 1, i = 5, temp,k=1;
srand(time(0));
num = rand() % 50 + 1;
// keep running the loop until the number has been guessed
printf("Guess the number between 1 to 50\n\n");
for(i=5;i>=1;i--){
c++;
scanf("%d",&guess);
if(guess>50 || guess<1){
printf("Please guess a number between 1 and 50\n\n");
}
else{
if (guess > num)
{
printf("You should enter a lower number\n");
printf("You have %d attempts remaining\n\n", (i - 1));
temp=i-1;
}
else if (guess < num)
{
printf("You should enter a higher number\n");
printf("You have %d attempts remaining\n\n", (i - 1));
temp=i-1;
}
else
{
printf("CONGRATULATIONS!! You won the game!!\n");
printf("You guessed it in %d attempts\n\n", (c-1));
return 0;
}
}
}
if(temp<1){
printf("Sorry!!\n");
printf("You have not got any more chances to guess the number\n");
printf("The number was: %d\n",num);
printf("Better luck next time!\n");
}
}