-
Notifications
You must be signed in to change notification settings - Fork 1
/
numberguessing.c
42 lines (40 loc) · 1.09 KB
/
numberguessing.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
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void guess(int N)
{
int num, guess, numberofguess = 0;
// Seed random number generator
srand(time(NULL)); // we don't need this if we pass an inputted N to guess and not an initialized variable
// Generate a random number
num = rand() % N; // int number = a + rand( ) % n; a = the first number in your range, n = the number of terms in your range(range computed by largest value - smallest value + 1)
printf("Guess a number between 1 and %d\nNote: You get only 9 tries!\n",N);
do {
if (numberofguess > 9)
{
printf("\nYou Lose!");
break;
}
scanf("%d", &guess);
if (guess > num)
{
printf("Lower number please!\n");
numberofguess++;
}
else if (num > guess)
{
printf("Higher number please!\n");
numberofguess++;
}
else
printf("You guessed the number in %d attempts!\n",numberofguess);
} while (guess != num);
}
void main()
{
int N;
printf("Enter the value of N for the range of guessing numbers: ");
scanf("%d",&N);
guess(N);
}