forked from dimpeshmalviya/C-Language-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumber_guessing.c
More file actions
32 lines (25 loc) · 773 Bytes
/
number_guessing.c
File metadata and controls
32 lines (25 loc) · 773 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
32
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int number, guess, attempts = 0;
// Initialize random number generator
srand(time(0));
number = rand() % 100 + 1; // random number between 1 and 100
printf("🎯 Welcome to Number Guessing Game!\n");
printf("I have chosen 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 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;
}