Creating a Number Guessing Game in C

Creating a Number Guessing Game in C

Creating a Number Guessing Game in C

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.

What Does the Game Do?

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

  1. Random Number Generation: The program generates a random number using the rand() function seeded with time(0) for unpredictability.
  2. Feedback Mechanism: After each guess, the player is informed if their guess is too high or too low, knowing them to adjust their next attempt.
  3. Attempt Limitation: Players are given a maximum of 10 attempts to guess the number, adding an element of challenge to the game.
  4. Win/Lose Conditions: The game ends either when the player guesses correctly or exhausts their attempts, displaying appropriate messages in both cases.

How the Code Works

  1. Random Number Generation: The srand() function initializes the random number generator, and the rand() function generates a number between 1 and 100.
  2. Player Input: The program uses scanf() to input the player’s guess and compare it to the generated number.
  3. Condition Checks: Conditional statements determine whether the guess is too high, too low, or correct, and provide feedback correctly.
  4. Loop for Attempts: A while loop keeps the game running until the player either guesses correctly or exhausts their attempts.

Source code of Simple Calculator Using C Language

// 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.