Welcome to the Number Guessing Game — a simple and fun C program that helps you learn key programming concepts such as loops, conditionals, input/output, and random number generation.
By studying and running this program, you will learn:
- How to generate random numbers in C
- Using variables and constants
- Taking user input with
scanf() - Using loops (
do...while) to repeat actions - Applying conditional statements (
if...else) for decision making - Counting attempts and giving user feedback
Number_Guessing_Game.c— The full source code for the game
#include <stdio.h> // For input/output functions like printf and scanf
#include <stdlib.h> // For random number functions rand() and srand()
#include <time.h> // For getting the current time to seed the random generatorThese libraries provide the necessary functions for input/output and random number generation.
int secretNumber, guess, attempts = 0;
const int MIN = 1, MAX = 100;secretNumber: Holds the random number the user tries to guessguess: Stores the user's current guessattempts: Counts how many tries the user has madeMINandMAX: Constants defining the guessing range (1 to 100)
srand(time(0));
secretNumber = (rand() % MAX) + MIN;srand(time(0))seeds the random number generator using the current time, so the number changes each time you run the programrand() % MAXgenerates a number between 0 and 99; addingMINshifts this range to 1–100
printf("Welcome to the Number Guessing Game!\n");
printf("I'm thinking of a number between %d and %d.\n", MIN, MAX);These lines greet the player and explain the rules.
do {
printf("Enter your guess: ");
scanf("%d", &guess);
attempts++;
if (guess > secretNumber) {
printf("Too high! Try again.\n");
} else if (guess < secretNumber) {
printf("Too low! Try again.\n");
} else {
printf("Congratulations! You guessed the number in %d attempts!\n", attempts);
}
} while (guess != secretNumber);- The program keeps asking for guesses until the correct number is found
- After each guess, it tells the user if their guess was too high, too low, or correct
- The
attemptscounter increments each time the user guesses
return 0;This signals the program finished successfully.
- Limit the number of guesses to 7 — tell the user if they run out of attempts
- Add a feature to ask if the user wants to play again after guessing correctly
- Introduce difficulty levels that change the
MAXnumber - Keep track of and display the best (lowest) number of attempts across games
gcc Number_Guessing_Game.c -o guessing_game
./guessing_gamegcc Number_Guessing_Game.c -o guessing_game.exe
guessing_game.exeCertainly! Here's a detailed, beginner-friendly README.md for your Simple Dice Roller program, tailored for educational purposes:
This is a basic C program that simulates rolling a six-sided dice. It generates random numbers to mimic dice rolls, displays the result with simple ASCII art, and lets the user roll again as many times as they want.
By studying and running this program, you will learn:
- How to generate random numbers in C
- Using variables to store and manage data
- Handling user input with
scanf() - Using loops (
do...while) to repeat actions until a condition is met - Creating simple ASCII art to display output nicely
- Understanding the importance of seeding the random number generator
Dice_Roller_Educational.c— The source code for the dice roller program
#include <stdio.h> // For input and output functions
#include <stdlib.h> // For random number functions
#include <time.h> // To seed the random number generator using the current timeThese libraries provide functions to read/write data and generate random numbers.
int diceValue; // Stores the value of the dice roll (1 to 6)
char rollAgain; // Stores user's decision to roll again ('y' or 'n')Variables hold data used throughout the program.
srand(time(0));- Seeds the random number generator using the current time,
- Ensures that the dice rolls are different each time the program runs.
do {
diceValue = (rand() % 6) + 1; // Generate a number between 1 and 6
// Display dice using ASCII art
// Show rolled number
// Ask user if they want to roll again
} while (rollAgain == 'y' || rollAgain == 'Y');- Generates a random dice roll
- Displays the dice visually with ASCII characters
- Takes input from the user to decide if they want to roll again
- Repeats the loop if user says 'y' or 'Y'
printf("\nThanks for playing! 🎉\n");
return 0;- Displays a friendly goodbye message
- Returns
0to signal successful program completion
- 🎲 Modify the program to roll two dice and show their sum.
- 🛡 Add input validation to accept only 'y', 'Y', 'n', or 'N'.
- 🔟 Change the dice to have 10 sides (values 1 to 10).
- 📊 Count how many rolls were made and display the total at the end.
- 🎨 Improve the ASCII art to better represent each dice face.
gcc Dice_Roller_Educational.c -o dice_roller
./dice_rollergcc Dice_Roller_Educational.c -o dice_roller.exe
dice_roller.exe