Automata Questions – 6

Q6. Check for syntax error/ logical error and correct the error to get the desired output.
Given n, print from n to 0
int main()
{
int n;
scanf(“%d”, &n);
unsigned int i = n;
while(i >= 0)
{
printf(“%d\n”, i);
i–;
}
return 0;
}
Input: 4

Output: Infinite loop
Answer: Error – Logical error
unsigned int i = n; unsigned integer ranges from 0 to 65535, which will be taken in the
cyclic order. So i– will keep repeating in a cyclic way. The loop will never be terminated.
So it should be written as int i = n;