Skip to content

Commit

Permalink
Merge pull request #57 from Kraizan/number_guesser_c
Browse files Browse the repository at this point in the history
added guess the number using C.
  • Loading branch information
debrajrout authored Oct 2, 2023
2 parents 36e6d93 + 134de51 commit 5d3b2f2
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions Number Guesser/guess_the_number.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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;
}

0 comments on commit 5d3b2f2

Please sign in to comment.