-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode3
More file actions
31 lines (24 loc) · 973 Bytes
/
code3
File metadata and controls
31 lines (24 loc) · 973 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
31
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number_to_guess, guess, attempts = 0;
srand(time(NULL)); // Seed the random number generator with current time
// Generate a random number between 1 and 100
number_to_guess = rand() % 100 + 1;
printf("Welcome to the Number Guessing Game!\n");
printf("I have selected a random number between 1 and 100.\n");
// Keep prompting the user for guesses until they guess the correct number
do {
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++; // Increment the number of attempts
if (guess < number_to_guess)
printf("Too low! Try again.\n");
else if (guess > number_to_guess)
printf("Too high! Try again.\n");
else
printf("Congratulations! You guessed the number %d in %d attempts.\n", number_to_guess, attempts);
} while (guess != number_to_guess);
return 0;
}