Skip to content
GATEiit

GATE CSE 2024 Session 1 Q18: Output of a C while loop with integer division

Question

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?

Try it: multiple choice: exactly one correct option

Select one option
Wrong answer costs 1/3 mark in GATE.
Show answer and explanation

Answer: C (official GATE 2024 final answer key)

Concept used: Integer division truncates toward zero.

First step: Trace one iteration: 6/12=06/12 = 0, so aa becomes 1, and bb stays 0.

Full step-by-step solution

Detailed working, common mistakes and linked mock tests will be available with a free GATEiit account. Log-in is coming soon.

Share this question

WhatsAppDownload image

Source: official GATE 2024 Session 1 CS question paper and final answer key, organised by IISc Bangalore. Question paper · Answer key