CodingMarket
Developer || Coding
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;
}
CodingMarket Developer || Coding
Welcome for coding world.
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.
Subscribe
We will notify you when anything happens in Indore.