From 134de516db9e4bb54885bc5c852a9650c8cec66a Mon Sep 17 00:00:00 2001 From: Vanshaj Bhatnagar Date: Mon, 2 Oct 2023 14:32:47 +0530 Subject: [PATCH] added guess the number using C. --- Number Guesser/guess_the_number.c | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 Number Guesser/guess_the_number.c diff --git a/Number Guesser/guess_the_number.c b/Number Guesser/guess_the_number.c new file mode 100644 index 0000000..37ec792 --- /dev/null +++ b/Number Guesser/guess_the_number.c @@ -0,0 +1,60 @@ +#include +#include +#include + +int main() +{ + srand(time(NULL)); // Seed for random number generation + int randomNumber = rand() % 100 + 1; // Generate random number between 1 and 100 + int userGuess, attempts = 0; + char playAgain; + + printf("Welcome to the Number Guessing Game!\n"); + printf("I have selected a random number between 1 and 100. Can you guess it?\n"); + + do + { + printf("Enter your guess: "); + scanf("%d", &userGuess); + attempts++; + + if (userGuess < 1 || userGuess > 100) + { + printf("Please enter a number between 1 and 100.\n"); + } + else if (userGuess < randomNumber) + { + printf("Too low! Try again.\n"); + } + else if (userGuess > randomNumber) + { + printf("Too high! Try again.\n"); + } + else + { + printf("Congratulations! You guessed the number %d in %d attempts.\n", randomNumber, attempts); + } + + // Flush the input buffer to prevent infinite loop on invalid input + while ((getchar()) != '\n') + ; + + } while (userGuess != randomNumber); + + printf("Do you want to play again? (y/n): "); + scanf(" %c", &playAgain); + + if (playAgain == 'y' || playAgain == 'Y') + { + // Generate a new random number for the next round + randomNumber = rand() % 100 + 1; + attempts = 0; + main(); // Restart the game + } + else + { + printf("Thank you for playing! Goodbye!\n"); + } + + return 0; +}