-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom_Number_Generator.c
More file actions
90 lines (78 loc) · 2.94 KB
/
Copy pathRandom_Number_Generator.c
File metadata and controls
90 lines (78 loc) · 2.94 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
// Seed the random number generator with the current time
srand(time(0));
// Generate a random number between 1 and 100
int random = (rand() % 100) + 1;
int n; // Variable to store the user's choice for limited or unlimited tries
int c = 0; // Counter for the number of attempts (used for unlimited tries)
int co = 1; // Counter for the number of attempts (used for limited tries)
// Display the menu for choosing between limited and unlimited tries
printf("Enter 1 if you want limited tries \n");
printf("Enter 2 if you want unlimited tries \n");
scanf("%d", &n);
// If the user chooses limited tries
if (n == 1) {
int no; // Variable to store the user's guess
// Loop until the user guesses the number or runs out of attempts
do {
printf("\nGuess the number: ");
scanf("%d", &no);
// Check if the guess is too high
if (co <= 10) {
if (no > random) {
printf("Lower number please");
printf("\n%d chances remaining", 10 - co);
}
// Check if the guess is too low
else if (no < random) {
printf("Higher number please");
printf("\n%d chances remaining", 10 - co);
}
// The guess is correct
else {
printf("Congrats! \n");
printf("You guessed the number in %d guesses", co);
}
co++; // Increment the attempt counter
// If the user has used all 10 attempts, end the game
if (co > 10) {
printf("\nAll your chances are used up!");
printf("\nThe correct guess is %d", random);
break;
}
}
} while (no != random);
}
// If the user chooses unlimited tries
else if (n == 2) {
int no; // Variable to store the user's guess
// Loop until the user guesses the number
do {
printf("\nGuess the number \n");
scanf("%d", &no);
// Check if the guess is too high
if (no > random) {
printf("Lower number please");
}
// Check if the guess is too low
else if (no < random) {
printf("Higher number please");
}
// The guess is correct
else {
printf("Congrats! \n");
}
c++; // Increment the attempt counter
} while (no != random);
// Display the number of attempts taken to guess the number
printf("You guessed the number in %d guesses", c);
}
// If the user enters an invalid choice
else {
printf("Wrong input, please re-enter the choice");
}
return 0;
}