09/08/2023
Play a single game in the C language, then calculate the sum of random numbers.
int main() {
int guess, target, sum = 0;
int attempts = 0;
// Seed the random number generator
srand(time(NULL));
// Generate a random target number between 1 and 100
target = rand() % 100 + 1;
printf("Welcome to the Guessing Game!\n");
printf("Try to guess the target number between 1 and 100.\n");
do {
printf("Enter your guess: ");
scanf("%d", &guess);
// Add the current guess to the sum
sum += guess;
if (guess < target) {
printf("Too low! Try again.\n");
} else if (guess > target) {
printf("Too high! Try again.\n");
}
attempts++;
} while (guess != target);
printf("Congratulations! You guessed the target number %d in %d attempts.\n", target, attempts);
printf("Sum of your guesses: %d\n", sum);
return 0;
}
Developer || Coding