forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber-guess-game.c
More file actions
28 lines (23 loc) · 758 Bytes
/
number-guess-game.c
File metadata and controls
28 lines (23 loc) · 758 Bytes
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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
srand(time(0)); // seed random number generator
number = rand() % 100 + 1; // random number between 1 and 100
printf("Welcome to the Number Guessing Game!\n");
printf("I'm thinking of a number between 1 and 100.\n");
do {
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("Too high! Try again.\n");
} else if (guess < number) {
printf("Too low! Try again.\n");
} else {
printf("Congratulations! You guessed it in %d attempts.\n", attempts);
}
} while (guess != number);
return 0;
}