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)
GATE CSE · Programming In C
Practice problems for Loops in Programming in C.
17 questions · 17 PYQs · 0 AI practice · GATE CSE 2027
🎯 These are sample questions
Just sign in to unlock everything. Free for all students.
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)
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)
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?
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 is
Consider the C program fragment below which is meant to divide x by y using repeated subtractions. The variables x, y, q and r are all unsigned int. while (r >= y) { r = r - y; q = q +1; } Which of the following conditions on the variables x, y, q and r before the execution of the fragment will ensure that the loop terminates in a state satisfying the condition x==(y*q+r)?
The following function computes the maximum value contained in an integer array p[] of size n (n>=1).
int max(int*p, int n) {
int a=0,b=n-1;
while (__________) {
if (p[a]<=p[b]) {
a=a+1;
} else {
b=b-1;
}
}
return p[a];
}
The missing loop condition is
The following function computes for positive integers X and Y.
int exp(int X,int Y) {
int res=1, a=X, b=Y;
while (b!=0) {
if(b%2==0) {
a=a*a;
b=b/2;
} else {
res=res*a;
b=b-1;
}
}
return res;
}
Which one of the following conditions is TRUE before every iteration of the loop?
Consider the following pseudo code, where x and y are positive integers. begin q := 0 r := x while r >= y do begin r := r - y q := q + 1 end end The post condition that needs to be satisfied after the program terminates is
Consider the following C function in which size is the number of elements in the array E:
int MyX(int *E, unsigned int size) {
int Y = 0;
int Z;
int i, j, k;
for(i = 0; i < size; i++) Y = Y + E[i];
for(i = 0; i < size; i++) for(j = i; j < size; j++) {
Z = 0;
for(k = i; k <= j; k++) Z= Z + E[k];
if (Z > Y) Y = Z;
}
return Y;
}
The value returned by the function MyX is the
Consider the following pseudo code. What is the total number of multiplications to be performed? D= 2 for i = 1 to n do for j = i to n do for k = j + 1 to n do D = D * 3
Consider the following segment of C-code: int j, n; j = 1; while (j <=n) j=j*2; The number of comparisons made in the execution of the loop for any is:
Consider the following C-function in which a[n] and b[m] are two sorted integer arrays and c[n+m] be another integer array.
void xyz (int a[],int b[],int c[]) {
int i, j, k;
i=j=k=0;
while((i < n) && (j < m)) if (a[i] < b[j]) c[k++]=a[i++];
else c[k++]=b[j++];
}
Which of the following condition(s) hold(s) after the termination of the while loop ? I. j m, k=n+j-1, and a [n-1] b[j] if i=n II. i n, k=m+i-1, and b[m-1] a[i] if j=m
Consider line number 3 of the following C-program.
int main ( ) {
/* Line 1 */ int I, N;
/* Line 2 */ fro (I =0, I < N, I++);
/* Line 3 */
}
Identify the compiler's response about this line while creating the object-module:
Consider the following C program main() { int x, y, m, n; scanf ("%d %d", &x, &y); /* Assume x > 0 and y > 0 */ m = x; n = y; while (m! = n) { if (m > n) m = m - n; else n = n - m; } print f ("% d", n); } The program computes
What does the following algorithm approximate? (Assume ). x = m; y = 1; while (x - y > e) { x = (x + y)/2; y = m/x; } print(x);
Consider the following program fragment for reversing the digits in a given integer to obtain a new integer. Let . int n, rev; rev = 0; while (n > 0) { rev = rev*10 + n%10; n = n/10; } The loop invariant condition at the end of the iteration is:
Consider the following C function.
float f(float x, int y) {
float p, s;
int i;
for (s=1, p=1, i=1; i < y; i ++) {
p*= x/i;
s+=p;
}
return s;
}
For large values of y, the return value of the function f best approximates
Want unlimited AI-generated Loops questions?
Sign up free and practice with adaptive difficulty — Easy, Medium, Hard. New questions every session.
Start practising for free →