In: Computer Science
In the following code, what values could be read into a number to terminate the while loop? PRINT "Enter a number: READ user input number ← user input WHILE (number < 1 or number > 10) PRINT "Enter another number: " READ user input number ← user input END WHILE
A) Numbers in the range 0 - 9
B) Numbers in the range 1 - 10
C) Numbers greater than 10
D) Numbers less than 1
Solution:
Option B is correct which is Numbers in range 1-10 (including 1 and 10)
Example code in C:
#include <stdio.h>
int main()
{
int n,an;
printf("Enter a number:");
scanf("%d",&n);
while(n<1 || n>10)
{
printf("enter another
number:");
scanf("%d",&n);
}
}
In output file first line tried range between 0-9 where loop still runs and next one numbers less than 1 and next one numbers greater than 10 and when I give input range between 1-10 (you can try even other numbers like 2,3,4..) then loop terminated.
Thank you, Have a great day:-)