Skip to content
Official PYQ · 2024

GATE CS 2024 Set 1 Q8 — Programming in C

Explain with AI

Consider the following C\mathrm{C} 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?

Choose one option
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 = 6
  • b = 0

Loop Condition: a < 10 (which is 6<106 < 10, evaluating to true).

Iteration 1:

  1. a = a / 12 + 1;
    Since a is an integer, 6 / 12 performs integer division and evaluates to 00.
    Thus, a = 0 + 1 = 1.
  2. a += b;
    Since b = 0, a = 1 + 0 = 1.

Iteration 2:

  • The condition a < 10 (1<101 < 10) is true.
  • a = a / 12 + 1;   ⟹  \implies a = 1 / 12 + 1 = 0 + 1 = 1.
  • a += b;   ⟹  \implies a = 1 + 0 = 1.

For all subsequent iterations, the value of a remains stuck at 11. Since 1<101 < 10 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.

Published
Report a correction
Keep exploringMore Programming in C questions