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.
#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;
}
Comments
Leave a comment