Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program is in C. Basically I am trying to guess a secret number (15), But t

ID: 3562625 • Letter: T

Question

This program is in C. Basically I am trying to guess a secret number (15), But the program doesnt recognize when i get it right. No compiling errors were there either so i am confused!

#include <stdio.h>
#include <stdlib.h>

int main()
{

int x = 15, y;

printf("Guess a number! ");
scanf("%i", &y);

while (y != x)
{


for (y=1; y<=14; y++)
{
printf("guess higher ");
scanf(" %i", &y );
}

for (y=16; y>=100; y++)

{
printf("guess lower ");
scanf(" %i", &y);
}
}
if (y == x)
{
printf("congrats ");
}

return 0;
}

Output (guess 15) (not coming out the way i want it too)

12 guess higher 18 guess lower 15 guess higher 22 guess lower 9 guess lower

Explanation / Answer

you need a while loop instead of for loops you have used.

also only writing

while (y<=14)

{
printf("guess higher ");
scanf(" %i", &y );
}

while (y>=16)

{
printf("guess lower ");
scanf(" %i", &y);
}

instead of

for (y=1; y<=14; y++)
{
printf("guess higher ");
scanf(" %i", &y );
}

for (y=16; y>=100; y++)

{
printf("guess lower ");
scanf(" %i", &y);
}

would be correct. :)

Happy to help!!!! CHEERS !!!

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote