Loop: GATE CS Previous Year Questions

Loop · Programming in C

17 questions · asked in 12 of 27 years (2000–2026)

MCQ 13 · MSQ 1 · NAT 31-mark 7 · 2-mark 10

2000Questions per year2026

Questions 1–5 of 17

Q1GATE 2025 (Session 1)2 marksNAT
Consider the following C program:

#include <stdio.h>  
int gate(int n) {  
    int d, t, newnum, turn;  
    newnum = turn = 0; t=1;  
    while(n >= t) t *= 10;  
    t /= 10;  
    while(t > 0) {  
        d = n / t;  
        n = n % t;  
        t /= 10;  
        if(turn) newnum = 10 * newnum + d;  
        turn = (turn + 1) % 2;  
    }  
    return newnum;  
}  
int main () {  
    printf("%d", gate(14362));  
    return 0;  
}
The value printed by the given C program is _________. (Answer in integer)

Q2GATE 2025 (Session 2)1 markNAT

int x=126,y=105;
 do {
	 if(x>y) x=x-y; 
	else y=y-x; 
      } while(x!=y); 

printf("%d",x); 
The output of the given C code segment is ________. (Answer in integer)

Q3GATE 2024 (Session 1)2 marksMSQ
Consider the following C function definition.

int f(int x, int y) {
for (int i=0; i < y; i++) {
x=x+x+y;
}
return x;
}
Which of the following statements is/are TRUE about the above function?

One or more options are correct.

Q4GATE 20192 marksNAT
Consider the following C program:

#include <stdio.h>
int main() {
 float sum = 0.0, j = 1.0, i = 2.0;
 while (i / j > 0.0625) {
    j = j + j; 
    sum = sum + i/j;
    printf("%f \n", sum);
 }
 return 0;
}
The number of times variable sum will be printed When the above program is executed is _________ .

Q5GATE 20182 marksMCQ−0.67 if wrong
Consider the following C code. Assume that unsigned long int type length is 64 bits.

unsigned long int fun(unsigned long int n){
unsigned long int i, j = 0, sum = 0;
for (i = n; i > 1; i = i/2) j++;
for ( ; j > 1; j = j/2) sum++;
return(sum);
}  
The value returned when we call fun with the input 2402^{40} is

Keep practising

Topics in Programming in C

All CS topics ›