{}
run-icon
main.c
#include <stdio.h> #include <stdlib.h> #include <time.h> void numberGuessingGame() { int secretNumber, guess, attempts = 0; // Seed random number generator srand(time(0)); secretNumber = rand() % 100 + 1; // Random number between 1 and 100 printf("Welcome to the Number Guessing Game!\n"); printf("I have selected a number between 1 and 100. Can you guess it?\n"); do { printf("Enter your guess: "); scanf("%d", &guess); attempts++; if (guess > secretNumber) { printf("Too high! Try again.\n"); } else if (guess < secretNumber) { printf("Too low! Try again.\n"); } else { printf("Congratulations! You guessed it in %d attempts.\n", attempts); } } while (guess != secretNumber); } int main() { char playAgain; do { numberGuessingGame(); printf("Do you want to play again? (y/n): "); scanf(" %c", &playAgain); } while (playAgain == 'y' || playAgain == 'Y'); printf("Thank you for playing! Goodbye!\n"); return 0; }
Output