Learn how to build a simple number guessing game in C using variables, loops, conditional statements, and random numbers. This beginner-friendly project helps you understand game logic and basic programming concepts step by step.
A number guessing game is a simple beginner project in C where the program chooses a random number and the player tries to guess it. It is a great way to practice variables, loops, conditions, user input, and random number generation in C.
In this project, the computer generates a number between 1 and 100. The user enters guesses one by one, and the program tells whether the guess is too high, too low, or correct.
How to take user input using scanf().
How to use if-else conditions for decision making.
How to repeat code using a loop.
How to generate random numbers with rand() and srand().
The program works in these steps:
Include the required header files such as stdio.h, stdlib.h, and time.h.
Generate a random number between 1 and 100.
Ask the user to enter a guess.
Compare the guess with the secret number.
If the guess is smaller, print “Too low”. If the guess is greater, print “Too high”. If the guess is equal, print a success message.
Continue until the correct number is guessed.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, guess, attempts = 0;
srand(time(0));
number = rand() % 100 + 1;
printf("Welcome to the Number Guessing Game!\n");
printf("I have selected a number between 1 and 100.\n");
do {
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++;
if (guess > number) {
printf("Too high! Try again.\n");
} else if (guess < number) {
printf("Too low! Try again.\n");
} else {
printf("Congratulations! You guessed the number in %d attempts.\n", attempts);
}
} while (guess != number);
return 0;
}
This program uses rand() % 100 + 1 to create a random number from 1 to 100, and srand(time(0)) helps make the number different in different runs. A do-while loop is used so the user can keep guessing until the answer is correct.
#include <stdio.h> is used for input and output functions like printf() and scanf().
#include <stdlib.h> is needed for rand() and srand().
#include <time.h> is used with time(0) to seed the random number generator.
attempts counts how many guesses the player makes.
The if-else statement checks whether the guess is high, low, or correct.
Welcome to the Number Guessing Game!
I have selected a number between 1 and 100.
Enter your guess: 50
Too low! Try again.
Enter your guess: 75
Too high! Try again.
Enter your guess: 63
Congratulations! You guessed the number in 3 attempts.
This output shows how the game guides the player after each guess until the correct number is found.
If you are using GCC, save the file as guess.c and use these commands:
gcc guess.c -o guess
./guess
On some systems, the executable may run as guess.exe instead. That depends on the compiler and operating system you use.
Possible improvements
You can make this project better by adding more features:
Limit the number of attempts.
Add difficulty levels such as easy, medium, and hard.
Let the user play again after winning.
Show the score based on the number of attempts.
What's next?
Apply your knowledge with one of our rigorous, hands-on internship programs.
Browse Internships