All Exams Test series for 1 year @ ₹349 only
Question

What is the output of the given C language code snippet? 

#include<stdio.h>

 void f(int n)

{

 if(n==0)

 return; 

printf("%d ",n); 

f(n-1); 

int main()

f(3); 

return 0; 

}

The correct answer is

3 2 1

This question tests your understanding of recursion and the use of the `printf()` function in C language.

Let's analyze the code snippet step-by-step:

  1. The program starts execution from the `main()` function, where the function `f()` is called with the argument `3`.
  2. Inside the `f()` function, the first statement is an `if` condition that checks whether `n` is equal to `0`. If `n` is `0`, the function returns immediately. Since `n` is `3`, the execution proceeds to the next statement.
  3. The `printf("%d ",n);` statement is executed, printing the current value of `n`, which is `3`, followed by a space.
  4. The function `f(n-1);` is called recursively, now with `n` as `2`.
  5. The same steps repeat: the `if` condition is checked and since `n` is not `0`, `printf("%d ",n);` prints `2`. Then `f(n-1);` is called again, this time with `n` as `1`.
  6. Repeating the process, `printf("%d ",n);` prints `1`. `f(n-1);` is called with `n` as `0`.
  7. When `n` is `0`, the `if(n==0)` condition evaluates to true and the function returns without printing anything further.

The recursive calls are completed, and the output generated is from the values of `n` that are printed in each recursive call.

Therefore, the output of the program is: 3 2 1

The correct answer is 3 2 1, which is obtained as described above.

Was this answer helpful?

Important Questions from Function Recursion

  1. The process of removing recursion involves replacing recursive function calls with:

  2. #include <stdio.h>
    
    int foo(int S[], int size){
        if(size == 0) return 0;
        if(size == 1) return 1;
        if(S[0] != S[1]) return 1 + foo(S + 1, size - 1);
        return foo(S + 1, size - 1);
    }
    
    int main(){
        int A[] = {0, 1, 2, 2, 2, 0, 0, 1, 1};
        printf("%d", foo(A, 9));
        return 0;
    }
    

    The value printed by the given C program is _______ . (Answer in integer)

  3. Consider the recursive functions represented by the following code segment:
    int bar(int n){
    if (n == 1) return 0;
    else return 1 + bar(n/2);
    }
    int foo(int n){
    if (n == 1) return 1;
    else return 1 + foo(bar(n));
    }

    The smallest positive integer n for which foo(n) returns 5 is ______. (answer in integer)
    Note: Ignore syntax errors (if any) in the function.
  4. Consider the following ANSI-C function.

    int func(int start, int end){
    int length=end+1-start;
    if((length < 1)||(start < 0)||(end < 0)){ return(0); }
    if(length%3==0){
    return(func(start+1, end));
    } else if(length%3==1){
    return(1+func(start, end-1));
    } else {
    return(func(start+2, end));
    }
    }


    The maximum possible value that can be returned from this function is ____________. (answer in integer)

    Note: Ignore syntax errors (if any) in the function.
  5. What is the value returned by the function f given below when n = 100 ?
    int f (int n)
    { if (n == 0) then return n;
    else
    return n + f(n-2);
    }
Need Expert Advice?
Test Series
RRB JE img
Railways
RRB JE Prev. Yr. Paper (CBT 1 + CBT 2) Test Series
290 Tests 5 Tests Free
4474 Attempts
4.3(88)
English

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App