Consider the following C program: #include <stdio.h> int main() { int a = 6;…

2024

Consider the following 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?

  1. A.

    The program prints 9 as output

  2. B.

    The program prints 10 as output

  3. C.

    The program gets stuck in an infinite loop

  4. D.

    The program prints 6 as output

Attempted by 419 students.

Show answer & explanation

Correct answer: C

Answer: The program gets stuck in an infinite loop.

Reasoning:

  • Initial values: a = 6, b = 0.

  • Inside the loop a is updated by a = a/12 + 1. Because this is integer division, 6/12 equals 0, so a becomes 1 after the first assignment.

  • Then a += b does not change a because b is 0, so a stays 1.

  • On the next iteration a is 1, and 1/12 is also 0 with integer division, so a remains 1. This repeats indefinitely, so the loop condition a < 10 stays true forever.

Conclusion: The loop never terminates and the program is stuck in an infinite loop.

Why the other outputs are incorrect:

  • Printing 9 or 10 is impossible because a never increases above 1 after the first iteration.

  • Printing 6 is incorrect because a is immediately changed from 6 to 1 in the first loop iteration.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir