Consider the following C program:
#include \(\lt\)stdio.h\(\gt\)
int main(){
int a = 6;
int b = 0;
while(a \(\lt\) 10) {
a = a / 12 + 1;
a += b;}
printf(”%d”, a);
return 0;}
Which one of the following statements is CORRECT?
GATE CS - 2024,
Answer: C
The program gets stuck in an infinite loop.
Trace it. a = 6, b = 0, and 6 \(\lt\) 10, so the loop runs: a = 6 / 12 + 1 = 0 + 1 = 1 (integer division), then a += 0 leaves a = 1. Now 1 \(\lt\) 10, so the loop runs again: a = 1 / 12 + 1 = 1, and a stays 1.
From then on a is 1 on every pass, so the condition a \(\lt\) 10 never becomes false and printf is never reached. The program prints nothing, so 9, 10 and 6 are all wrong; 6 would require the loop body never to run.
Please log in or register to report this question.