GATE CS 2024 Set 1 — Question 18

MCQ+1 / -0.33EasyControl FlowC ProgrammingProgramming & Data Structures

Programming & Data Structures → C Programming → Control Flow

Last updated

Question

Consider the following C program:
#include 
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?
A.
The program prints 9 as output
B.
The program prints 10 as output
C.
The program gets stuck in an infinite loop
D.
The program prints 6 as output

Correct answer

(C) The program gets stuck in an infinite loop

Solution

Let's trace the execution of the program step-by-step:
1.Initialization: The variables are initialized as a=6a = 6 and b=0b = 0.
2.Loop Condition: The while loop continues as long as a<10a < 10.
3.First Iteration:
  • The condition 6<106 < 10 is true.
  • a = a / 12 + 1: In C, integer division 6/126 / 12 results in 00. Thus, a=0+1=1a = 0 + 1 = 1.
  • a += b: a=1+0=1a = 1 + 0 = 1.

4. Second Iteration:
  • The condition 1<101 < 10 is true.
  • a = a / 12 + 1: Integer division 1/121 / 12 results in 00. Thus, a=0+1=1a = 0 + 1 = 1.
  • a += b: a=1+0=1a = 1 + 0 = 1.
In every subsequent iteration, the value of aa will remain 11. Since 11 is always less than 1010, the loop condition a<10a < 10 will always be true, and the program will never exit the loop. Therefore, the program gets stuck in an infinite loop.

More questions on C Programming

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free