CodingMarket

CodingMarket Developer || Coding

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

09/08/2023

Welcome for coding world.

Address

Indore
Indore
452001

Alerts

Be the first to know and let us send you an email when CodingMarket posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Shortcuts

Share