Question

Write a C program that generates a random number between 0 and 50. Then, the program...

Write a C program that generates a random number between 0 and 50. Then, the program prompts for guesses until the user is correct, or has made 10 wrong guesses. After each guess, the program indicates whether the guess was too high, too low, or correct. Once the round has ended, either by a correct guess or by using up the 10 guesses, the program displays the current status.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(time(NULL));
    int goal = rand() % 50 + 1, guess, i;
    for(i = 0; i < 10; ++i) {
        printf("Enter your guess (between 1 and 50): ");
        scanf("%d", &guess);
        if(guess < goal) {
            printf("Too low ...\n");
        } else if(guess > goal) {
            printf("Too high ...\n");
        } else {
            printf("Correct, the number was %d.\n", goal);
            break;
        }
        if(i == 9) {
            printf("Sorry, the number was %d\n", goal);
        }
    }
    return 0;
}



Add a comment
Answer #2

Sure, here's a C program that implements the described game:

cCopy code#include <stdio.h>#include <stdlib.h>#include <time.h>int main() {    // Seed the random number generator
    srand(time(0));    // Generate a random number between 0 and 50
    int randomNumber = rand() % 51; // Generates a number from 0 to 50

    int guess, attempts = 0;    char response;    printf("Guess the random number between 0 and 50.\n");    while (attempts < 10) {        printf("Enter your guess: ");        scanf("%d", &guess);

        attempts++;        if (guess == randomNumber) {            printf("Congratulations! You guessed the correct number in %d attempts.\n", attempts);            break;
        } else if (guess < randomNumber) {            printf("Too low! ");
        } else {            printf("Too high! ");
        }        printf("You have made %d out of 10 attempts. Try again? (y/n): ", attempts);        scanf(" %c", &response);        if (response != 'y' && response != 'Y') {            break;
        }
    }    if (attempts >= 10) {        printf("Sorry, you have reached the maximum number of attempts. The correct number was %d.\n", randomNumber);
    }    return 0;
}

This program will generate a random number between 0 and 50 and prompt the user to make guesses. It will indicate whether the guess is too high, too low, or correct, and it will keep track of the number of attempts. The game will end either when the user guesses correctly or when they have made 10 wrong guesses. At the end of the game, it will display the current status.


answered by: Hydra Master
Add a comment
Know the answer?
Add Answer to:
Write a C program that generates a random number between 0 and 50. Then, the program...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • Write a C++ program that generates a random number from 0 to 9. It will then...

    Write a C++ program that generates a random number from 0 to 9. It will then prompt the user to guess the random number it generated. Your program stops if the user guesses the right number. (Use the cout function to display the random number the computer generates so that you can determine if your code actually works). SAMPLE OUTPUT Random number = 8 Guess a number in the range [0,9]: 1 Too low Guess a number in the range...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts...

    Write a game application that generates a random number between 1 and 1000 (inclusive) and prompts the user for a guess. Use the pseudorandom number generator, the Random class. When the user guesses the correct number, say so, and display how many guesses the user required to guess correctly. Allow the user to play multiple times by asking the user if the user wants to continue playing.      Guess my number between 1 and 1000 => 500      Too high...

  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

  • Use visual studio C# Tasks You are going to build a program that generates a random...

    Use visual studio C# Tasks You are going to build a program that generates a random integer between 1 and 10, then prompts user to make several guesses of the number until user gets the right number - For each guess, tell user if the guess is higher or lower than the number - Handle the potential exception with user guess. Sample Output DAC programPrajects ExameExam1bin,DebugExarm1.exe Welcome! Guess the number I'm thinking of . It is between 1 and 1e...

  • Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, al...

    Lab #6 Number Guessing Game – Loops and Nested Loops Requirements: Design, develop, test, and submit a program for a Number Guessing game. The program will select a random number between 0 and 100, allow the user to try up to 10 times to guess the number, and tell the user if each guess is too high, too low, or correct. The actual game portion must be a function...you can use more than one function in your program. The amount...

  • Write a Java Program to allow a user to guess a person’s age until they get...

    Write a Java Program to allow a user to guess a person’s age until they get it correct. After each guess, the user must be told whether their guess was correct, too high or too low. Once the user enters the correct age, the program must display how many guesses it took the user to guess the correct age.

  • This program will implement a simple guessing game... Write a program that will generate a random...

    This program will implement a simple guessing game... Write a program that will generate a random number between 1 and 50 and then have the user guess the number. The program should tell the user whether they have guessed too high or too low and allow them to continue to guess until they get the number or enter a 0 to quit. When they guess the number it should tell them how many guesses it took. At the end, the...

  • Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the...

    Using C++ language P2-4 Write a program that randomly generates an integer and then prompts the user to guess the number. If the user guesses the number correctly, the program outputs an appropriate message such as "You win!" and terminates. Otherwise, the program checks and tell the user whether the guessed number is less or greater than the target number and then prompts him for another try. However, the program gives the user five tries only to guess the correct...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT