-
Notifications
You must be signed in to change notification settings - Fork 0
/
numberGuessingGame.c
executable file
·50 lines (41 loc) · 1007 Bytes
/
numberGuessingGame.c
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
/*
*NumberGuessingGame
*This program creates a random number between 1 and 100
*It lets you guess which number has been created
*Returns the number of needed guesses
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
int num;
int cNum;
int high = 100;
int low = 1;
int count = 0;
time_t t;
/*Initialize random number generator*/
srand((unsigned int)t);
printf("Choosing random number...\n");
/*Choose random number to be guessed*/
cNum = (rand()%(high-low+1))+low;
printf("Random number chosen.\n");
printf("Please insert your guess: \n");
while (cNum){
scanf("%i", &num);
count += 1;
if (cNum > num) {
printf("You guessed too low. Try again.");
} else if (cNum < num) {
printf("You guessed too high. Try again.");
} else {
printf("Correct the chosen random number was ");
printf("%i", cNum);
printf(". You needed to guess ");
printf("%i", count);
printf(" times to get there.\n\n\n\n\n\n\n");
break;
}
}
return(0);
}