Consider the following program:
#include <stdio.h>
int main() {
int a=6;
int b = 0;
while (a<10) {
a = a / 12+1 ;
a += b ;}
printf ("%d", a);
return 0 ; }
Which one of the following statements is CORRECT?
View worked solution
Answer: C
To find the output of the given C program, let's trace the execution step-by-step:
Initial State:
a = 6b = 0
Loop Condition: a < 10 (which is , evaluating to true).
Iteration 1:
a = a / 12 + 1;
Sinceais an integer,6 / 12performs integer division and evaluates to .
Thus,a = 0 + 1 = 1.a += b;
Sinceb = 0,a = 1 + 0 = 1.
Iteration 2:
- The condition
a < 10() is true. a = a / 12 + 1;a = 1 / 12 + 1 = 0 + 1 = 1.a += b;a = 1 + 0 = 1.
For all subsequent iterations, the value of a remains stuck at . Since is always true, the while loop will execute indefinitely.
Therefore, the program gets stuck in an infinite loop.
Free practice · Your answers on this page are not saved.
