Physical Address
Godawari 2 Attariya Kailali
Physical Address
Godawari 2 Attariya Kailali
Games are a fun and interactive way to practice programming concepts. The Number Guessing Game is a classic project that enhances your skills in loops, conditionals, and random number generation. This article will discuss creating a simple number-guessing game using C. It’s a perfect project for beginners who want to combine logical thinking with coding.
The Number Guessing Game generates a random number between 1 and 100, and the player’s task is to guess the number within a limited attempt. The program provides feedback for each guess, indicating whether the guess was too high, too low, or correct. If the player runs out of attempts, the game ends, revealing the correct number.
Key Features
rand()
function seeded with time(0)
for unpredictability.srand()
function initializes the random number generator, and the rand()
function generates a number between 1 and 100.while
loop keeps the game running until the player either guesses correctly or exhausts their attempts.// Guessing the number project using C #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int numberToGuess, userGuess, attempts = 0; const int maxAttempts = 10; // Seed the random number generator srand(time(0)); // Generate a random number between 1 and 100 numberToGuess = (rand() % 100) + 1; printf("Welcome to the Number Guessing Game!\n"); while (1) { printf("Enter your guess (1-100): "); scanf("%d", &userGuess); attempts++; if (userGuess == numberToGuess) { printf("Congratulations! You guessed the number in %d attempts.\n", attempts); break; } else if (userGuess < numberToGuess) { printf("Too low. Try again.\n"); } else { printf("Too high. Try again.\n"); } if (attempts >= maxAttempts) { printf("Sorry, you've run out of attempts. The number was %d.\n", numberToGuess); break; } } return 0; }
Conclusion
The number guessing game is an easy and fun project. It helps you learn programming concepts like loops, conditions, and random number generation. You can also learn how to take input from users and give feedback. Try building and changing this project to improve your coding skills and enjoy coding!
Notes: If you have any Kind of problem on the project Contact us on Social Media.