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