Skip to content

Instantly share code, notes, and snippets.

@Caalek
Created March 13, 2021 22:27
Show Gist options
  • Save Caalek/cb62ee33608ae9687eaf303e25bbe681 to your computer and use it in GitHub Desktop.
Save Caalek/cb62ee33608ae9687eaf303e25bbe681 to your computer and use it in GitHub Desktop.
Numer guessing game in C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
srand(time(NULL));
int guessRange = 50;
int number = rand() % guessRange + 1;
int guess;
int attempts = 0;
bool guessed = false;
while (!guessed)
{
printf("Guess a number between 0 and %d: ", guessRange);
scanf("%d", &guess);
if (guess < number)
{
++attempts;
printf("To small!\n");
}
else if (guess > number)
{
++attempts;
printf("Too big!\n");
}
else if (guess == number)
{
++attempts;
printf("You guessed in %d attempts!\n", attempts);
guessed = true;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment