#MonthOfCode - Day 7: input

My entry for the 7th day of the month of code. The theme for today is: input.

One of the first program I wrote after the mandatory hello world was probably a “Guess the number” game in C. It picks a random number and reads the user input until the guess is correct.

Code after the break.

input.cview raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main() {
srand(time(NULL));

int n = 1 + (rand() % 10);
int g;

printf("Guess a number between 1 and 10.\n");

while (1) {
scanf("%d", &g);
if (g == n) {
printf("Correct!\n");
exit(0);
} else if (g < n) {
printf("It's more\n");
} else {
printf("It's less\n");
}
}
}