How does this code restrict entry to a number between 0 and 4?

Asked

Viewed 223 times

3

I was answering this question, but it doesn’t make sense to me.

Código

For me I would have to relate values less than 0 and greater than 4. I understood that I understood the inverse of alternative C.

  • If you execute the code you will understand better. If you enter a number greater than 4 or less than 0, the program asks to enter a valid number.

1 answer

4


The correct answer is C.

The program calculates the factorial

Clearly the program is doing this - Checked

of a given value in the range of 0 to 4 inclusive (sic)

while(n<0 || n>4)
scanf("%d", &n);

This excerpt does just this. It keeps repeating the request while a number between the defined interval is not typed. When something between 0 and 4 is typed the code exits the loop and continues the execution by calculating the factorial of the number typed right after. This section only has the function of insisting on a valid number - Checked

not calculating the factorial for other values that are not in this range

Since the code does not exit the loop if you enter other values, it cannot calculate the factorial for other values. - Checked

The option d clearly it cannot be because it claims to accept the number 5 that it is obvious that it does not accept.

The option b would only be possible if it had two loops inside each other, the internal to calculate the factorial and the external to vary the numerical sequence.

The option a would only be viable if the c was wrong too, which is not the case.

The code is poorly written and does not seem to teach anything useful, probably you should escape from materials like this.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.