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

Posted on

Question :

I was answering this question, but it does not make sense to me.

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


Answer :

The correct answer is C.


The program calculates the factorial

Clearly the program is doing this – Checked


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

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

This passage does just that. It keeps repeating the request until a number in the defined range is not entered. When something is typed between 0 and 4 the code exits the loop and continues execution by calculating the factorial of the number entered shortly. 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

Because the code does not leave the loop if you enter other values, it can not calculate the factorial for other values. – Checked

The d option clearly can not because it claims to accept the number 5 that it obviously does not accept.

The b option would only be possible if you had two loops one inside the other, the internal one to calculate the factorial and the external one 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. You should probably get away with materials like this.


Leave a Reply

Your email address will not be published. Required fields are marked *