edited by
347 views
2 votes
2 votes

Consider the below function:

int s (int n)
{
if (n<=1) return 1 ;
n = (n-1) * (n-1) -2 - n * n + 3*n ;
s(n);
printf(“%d”, n );
}

What is the output if initial call is $s(6)$ ?

  1. $55555$
  2. $11111$
  3. $54321$
  4. $12345$
edited by

1 Answer

Best answer
3 votes
3 votes
Correct option is (D).

s(6) =  (6-1)^2 -2 - 6^2 + 3*6  = 5 and before printf, s(5) is called.

s(5) =  (5-1)^2 -2 - 5^2 + 3*5  = 4 and before printf, s(4) is called.

s(4) =  (4-1)^2 -2 - 4^2 + 3*4  = 3 and before printf, s(3) is called.

s(3) =  (3-1)^2 -2 - 3^2 + 3*3  = 2 and before printf, s(2) is called.

s(2) =  (2-1)^2 -2 - 2^2 + 3*2  = 1 and before printf, s(1) is called.

s(1) will simply return and won't print anything.

Therefore, printf will be executed in reverse order, i.e. s(2)->s(3)->s(4)->s(5)->s(6)

Hence, output = 12345.
selected by
Answer:

Related questions

3 votes
3 votes
1 answer
1
Bikram asked May 14, 2017
392 views
Assume that $a[4]$ is a one-dimensional array of $4$ elements, $p$ is a pointer variable and $p = a$ is performed. Now, which among these expressions is illegal?$p == a[0...
0 votes
0 votes
1 answer
2
Bikram asked May 14, 2017
351 views
Spot the error(s) in this code snippet :int n=2; // Line 1 switch(n) { case 1.5: printf( "gate"); break; case 2: printf( "overflow"); break; case 'A': printf("gateoverflo...
0 votes
0 votes
1 answer
3
Bikram asked May 14, 2017
200 views
#include<stdio.h int K = 10; int main() { foo(); foo(); return 0; } int foo() { static int k= 1; printf("%d ",k); k++; return 0; }What is the output of the above code sni...
0 votes
0 votes
1 answer
4
Bikram asked May 14, 2017
203 views
What is the output of the following program? int fun (int z) { if( z==0 || z==1 ) return 1; else return fun(z-1) ; } int main() { int y; y=fun(8); printf(“%d”...