-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_guessing_game.c
45 lines (36 loc) · 1.3 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
const int MAX_ATTEMPTS = 10;
// Initialize random number generator
srand(time(NULL));
number = rand() % 100 + 1;
printf("====================================\n");
printf(" Welcome to the Number Guessing Game!\n");
printf("====================================\n");
printf("I have selected a number between 1 and 100.\n");
printf("Can you guess what it is?\n\n");
while (attempts < MAX_ATTEMPTS) {
printf("Attempt %d/%d\n", attempts + 1, MAX_ATTEMPTS);
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("Your guess is too high! Try again.\n\n");
} else if (guess < number) {
printf("Your guess is too low! Try again.\n\n");
} else {
printf("Congratulations! You guessed the number %d in %d attempts.\n", number, attempts);
break;
}
if (attempts == MAX_ATTEMPTS) {
printf("Sorry, you've reached the maximum number of attempts.\n");
printf("The number was %d.\n", number);
}
}
printf("\nGame Over\n");
printf("====================================\n");
return 0;
}