-
Notifications
You must be signed in to change notification settings - Fork 19
/
guessing game
78 lines (62 loc) · 1.21 KB
/
guessing game
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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function that generate a number in
// the range [1, N] and checks if the
// generated number is the same as the
// guessed number or not
void guess(int N)
{
int number, guess, numberofguess = 0;
//Seed random number generator
srand(time(NULL));
// Generate a random number
number = rand() % N;
printf("Guess a number between"
" 1 and %d\n",
N);
// Using a do-while loop that will
// work until user guesses
// the correct number
do {
if (numberofguess > 9) {
printf("\nYou Loose!\n");
break;
}
// Input by user
scanf("%d", &guess);
// When user guesses lower
// than actual number
if (guess > number)
{
printf("Lower number "
"please!\n");
numberofguess++;
}
// When user guesses higher
// than actual number
else if (number > guess)
{
printf("Higher number"
" please!\n");
numberofguess++;
}
// Printing number of times
// user has taken to guess
// the number
else
printf("You guessed the"
" number in %d "
"attempts!\n",
numberofguess);
} while (guess != number);
}
// Driver Code
int main()
{
int N = 100;
// Function call
guess(N);
return 0;
}