Answer to Question #342540 in C for Anonymous

Question #342540

Create a program based on the attached output using a for loop.


Output:

Enter hidden number: 50

Set of how many attempts to allow the user to guess: 5

Enter your guess number: 45

Sorry! Your guessed number is lesser than the hidden number.

You have 4 attempts left.

Enter your guess number: 51

Sorry! Your guessed number is greater than the hidden number.

You have 3 attempts left.

Enter your guess number: 10

Sorry! Your guessed number is lesser than the hidden number.

You have 2 attempts left.

Enter your guess number: 17

Sorry! Your guessed number is lesser than the hidden number.

You have 1 attempts left.

Enter your guess number: 49

Sorry! Your guessed number is lesser than the hidden number.

You have 0 attempts left.

Sorry you failed to guess the number please try again later.


1
Expert's answer
2022-05-19T08:56:49-0400
#include <stdio.h>


int main()
{
	int num, attempts, guess;

	printf("Enter hidden number: ");
	scanf("%d", &num);
	printf("Set of how many attempts to allow the user to guess: ");
	scanf("%d", &attempts);

	while (attempts > 0)
	{
		printf("Enter your guess number: ");
		scanf("%d", &guess);
		if (guess < num)
		{
			printf("Sorry! Your guessed number is lesser than the hidden number.\n");
		}
		else if (guess > num)
		{
			printf("Sorry! Your guessed number is greater than the hidden number.\n");
		}
		else
		{
			printf("Yes! You guessed.\n");
			break;
		}

		attempts--;
		printf("You have %d attempts left.\n", attempts);
	}

	if (attempts == 0)
	{
		printf("Sorry you failed to guess the number please try again later.\n");
	}

	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS