Daily C Language Challenge No. 10: Number Guessing Game – Can You Guess It in 10 Attempts?

📌 Problem Description

Write a number guessing game with the following rules::

  1. The program randomly generates an integer between 1~100.

  2. The user has a maximum of 10 attempts, with hints provided as “too high” or “too low” after each guess.

  3. Upon guessing correctly, a congratulatory message is displayed; if unsuccessful, the correct answer is revealed.

// Example: 1st guess: 50 → Hint: too low  3rd guess: 75 → Hint: too high  5th guess: 63 → Congratulations! You guessed it in 5 attempts!

Additional Knowledge:

InC language, the rand function and srand function are primarily used for generating random numbers. Here is a brief summary of the functionalities of these two functions: The rand functionFunction: Used to generate pseudo-random integers.Prototype:int rand(void);Return Value: Returns a pseudo-random integer in the range of 0 to RAND_MAX.RAND_MAX is a constant defined in the <stdlib.h> header file, with a value of at least 32767.Usage Example:

#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt; int main() {    int random_num = rand();    printf("The generated random number is: %d\n", random_num);    return 0; }

Note: If thesrand function is not used to set the random seed, the sequence of random numbers generated by the rand function will be the same each time the program is run.The srand function

Function: Sets the random seed, which the rand function uses to generate the sequence of random numbers.Different seeds will result inthe rand function generating different sequences of random numbers.Prototype:void srand(unsigned int seed);Parameter:seed is an unsigned integer that serves as the seed for random number generation.Usage Example:

#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;time.h&gt; int main() {    // Use current time as seed    srand((unsigned int)time(NULL));    int random_num = rand();    printf("The generated random number is: %d\n", random_num);    return 0; }

Note: It is common to usetime(NULL) as the parameter for the srand function, as time(NULL) returns the current time in seconds, which changes every second, allowing for different sequences of random numbers each time the program is run.

Difficulty: ⭐️⭐️ (suitable for learners mastering loops and random numbers)

💡 Basic Implementation (Complete Code)

#include &lt;stdio.h&gt;#include &lt;stdlib.h&gt;#include &lt;time.h&gt;
int main() {    srand(time(0)); // Initialize random seed    int target = rand() % 100 + 1; // Generate random number between 1 and 100    int guess, attempts = 0;
    printf("Number Guessing Game (1~100), you have 10 attempts!\n");
    while (attempts &lt; 10) {        printf("Guess attempt %d: ", attempts + 1);        scanf("%d", &amp;guess);        attempts++;
        if (guess == target) {            printf(" Congratulations! You guessed it in %d attempts!\n", attempts);            return 0;        } else if (guess &lt; target) {            printf("Hint: too low\n");        } else {            printf("Hint: too high\n");        }    }
    printf(" Challenge failed! The correct answer is: %d\n", target);    return 0;}

🔥 Advanced Optimization (Enhancing Interactivity)

1. Input Validation (Preventing Non-numeric Input)

while (1) {    printf("Guess attempt %d: ", attempts + 1);    if (scanf("%d", &amp;guess) != 1) { // Clear buffer on non-numeric input        printf("Please enter a valid number!\n");        while (getchar() != '\n'); // Clear input buffer        continue;    }    break;}

2. Remaining Attempts Hint

printf("Remaining attempts: %d\n", 10 - attempts);

3. Difficulty Levels (Expanding Gameplay)

int max_attempts, range;printf("Choose difficulty level:\n1. Easy (15 attempts, 1~50)\n2. Normal (10 attempts, 1~100)\n3. Hard (5 attempts, 1~200)");scanf("%d", &amp;difficulty);switch (difficulty) {    case 1: max_attempts=15; range=50; break;    case 2: max_attempts=10; range=100; break;    case 3: max_attempts=5; range=200; break;}target = rand() % range + 1;

🤔 Common Error Analysis

Issue Cause Solution
Same random number on each run Random seed not initialized correctly <span>srand(time(0))</span> should be placed outside the loop
Inputting letters causes infinite loop Non-numeric input not handled Use<span>scanf</span> return value check and clear the buffer
Hint logic error Comparison operators reversed (e.g., using<span>></span> instead of<span><</span>) Carefully check the order of conditions

🚀 Next Issue Preview:No. 11: Array Reversal Output – How Many Methods Can You Implement?

📢 Interaction Time

How many attempts did it take you to guess the number? Feel free to leave a comment and share your results!

🚀 If you find this interesting, feel free to share it with friends learningC language!

Article Author:Vv Computer Graduate World (focusing on computer graduate exam tutoringfor 8 years)

Original Statement: Please contact for authorization before reprinting; infringement will be pursued.

Leave a Comment