Views: 8
INTRODUCTION
The Number Guessing Game is a simple yet engaging program designed to test and improve the user’s logical thinking and problem-solving skills. It combines basic concepts of programming logic, loops, and conditional statements to create an interactive experience.
Features of the Game:
- Random Number Generation: The game generates a random number between 1 and 100, ensuring each session is unique.
- User Interaction: It allows the user to repeatedly guess the number, providing feedback on whether the guessed number is too high, too low, or correct.
- Dynamic Gameplay: The program tracks the number of attempts taken by the user to guess correctly and displays it at the end.
- Real-Time Feedback: After every guess, the game provides hints, guiding the user toward the correct answer.
Purpose of the Game:
- Learning Tool: Serves as a great project for beginners to practice key programming concepts like randomization, loops, and conditions.
- Fun and Challenge: Offers an entertaining way to challenge oneself in predicting numbers within the shortest number of guesses.
CODE SNIPPETS
#include <stdio.h>
#include <stdlib.h> // For rand() and srand()
#include <time.h> // For time()
int main()
{
// Initialize random number generator with current time as seed
srand(time(0));
// Generate a random number between 1 and 100
int randomNumber = (rand() % 100) + 1;
int guessed_number;
int no_of_guesses = 0;
do
{
printf("Enter your number : ");
scanf("%d", &guessed_number);
if (guessed_number > randomNumber)
{
printf("Lower Number Please !\n");
}
else if (guessed_number < randomNumber)
{
printf("Higher Number Please !\n");
}
else
{
printf("Congrats,You Guessed it Right !\n");
}
no_of_guesses++;
} while (guessed_number != randomNumber);
// Print the random number
// printf("Random number between 1 and 100: %d\n", randomNumber);
printf("You guessed in %d guesses\n",no_of_guesses);
return 0;
}
OUTPUT

Code Explanation
#include <stdio.h>
#include <stdlib.h> // For rand() and srand()
#include <time.h> // For time()
Introduction:
#include <stdio.h>
: Provides input/output functions (printf
, scanf
).
#include <stdlib.h>
: Includes rand()
for generating random numbers and srand()
for seeding randomness.
#include <time.h>
: Provides time()
to seed the random number generator dynamically.
int main()
Purpose: The entry point of the program where execution begins.
srand(time(0));
Functionality:
- Seeds the random number generator with the current system time.
- Ensures different random numbers are generated each time the program runs.
int randomNumber = (rand() % 100) + 1;
Purpose:
- Generates a random integer between 1 and 100.
rand() % 100
: Limits the random number range to 0–99.
+ 1
: Shifts the range to 1–100.
int guessed_number;
int no_of_guesses = 0;
Variable Initialization:
guessed_number
: Stores the user’s input during the game.
no_of_guesses
: Counts the total number of attempts the user takes to guess correctly.
do
{
printf("Enter your number : ");
scanf("%d", &guessed_number);
Gameplay Loop:
- A
do-while
loop is used to repeatedly prompt the user for guesses until the correct number is guessed.
printf
: Prompts the user to input a number.
scanf
: Reads the user’s input and assigns it to guessed_number
.
if (guessed_number > randomNumber)
{
printf("Lower Number Please !\n");
}
- Checks if the guessed number is greater than the random number.
- If true, prompts the user to guess a lower number.
else if (guessed_number < randomNumber)
{
printf("Higher Number Please !\n");
}
- Checks if the guessed number is less than the random number.
- If true, prompts the user to guess a higher number.
else
{
printf("Congrats,You Guessed it Right !\n");
}
- Triggered when the guessed number matches the random number.
- Congratulates the user for guessing correctly.
no_of_guesses++;
Purpose:
- Increments the
no_of_guesses
counter after each attempt, regardless of the outcome.
} while (guessed_number != randomNumber);
printf("You guessed in %d guesses\n", no_of_guesses);
Loop Continuation:
- The
do-while
loop continues until the user’s guess matches the random number.
End of Game:
- Displays the total number of attempts the user took to guess the random number correctly.
How the Program Works
- Game Start:
- A random number is generated between 1 and 100.
- The user is prompted to guess the number.
- Gameplay:
- The program provides hints after each guess, indicating whether the user should guess higher or lower.
- The process repeats until the correct number is guessed.
- End of Game:
- The program congratulates the user and displays the total number of attempts made.
Improvements
- Error Handling:
- Add checks to ensure the user inputs valid integers.
- Handle scenarios where input is non-numeric to avoid program crashes.
- Dynamic Range:
- Allow the user to select the range for the random number (e.g., 1–1000).
- Enhanced Feedback:
- Add encouraging messages for near guesses.
- Include a leaderboard to track the fewest guesses for multiple players.
LINKS :
Numberle – A daily number game – GitHub Pages
Number guessing game in C
Guess the Number Game in JavaScript