Created
March 13, 2021 22:27
-
-
Save Caalek/cb62ee33608ae9687eaf303e25bbe681 to your computer and use it in GitHub Desktop.
Numer guessing game in C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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