CrazyEngineers
  • What will be the output of this program ?

    Updated: Oct 26, 2024
    Views: 7.8K

    please give the complete detailed solution of this program ..
    #include<stdio.h> void fun(int); typedef int (*pf) (int, int); int proc(pf, int, int); int main() { int a=3; fun(a); return 0; } void fun(int n) { if(n > 0) { fun(--n); printf("%d,", n); fun(--n); } }

    Here's a detailed analysis of your program. Before proceeding, there seem to be syntax errors in your code:

    1. You didn't include a library. There's a #include directive but it doesn't specify any library.

    2. The typedef line is not complete and it's not used anywhere in the program.

    After addressing the errors, the remaining part of the program is:

    #include <stdio.h>
    
    void fun(int);
    
    int main() {
    
        int a=3;
    
        fun(a);
    
        return 0;
    
    }
    
    void fun(int n) {
    
        if(n > 0) {
    
            fun(--n);
    
            printf("%d,", n);
    
            fun(--n);
    
        }
    
    }

    This program does the following:

    1. It includes the standard input/output library (stdio.h).

    2. It declares a function fun(int) that doesn't return a value (`void`). This function is defined later in the program.

    3. int main() is the entry point for the program.

    4. Inside main(), an integer a is declared and initialized with the value 3.

    5. The function fun(a) is called with a as its argument.

    6. The program then returns 0 as a standard way to indicate that it finished successfully.

    7. The function fun(int n) is defined. It takes an integer n as input.

    8. Inside fun(int n), there's a conditional check. If n is greater than zero, the program:

    - Calls fun(--n). --n is a pre-decrement operator which decreases n by 1 before the function call.

    - Then, it prints the value of n to the console.

    - Calls fun(--n) again, further decreasing n by 1 before the function call.

    The function fun(int n) is a recursive function. It calls itself until n becomes zero, which is the base case to stop the recursion.

    The result of this program will be the output of all the printf statements in the recursion tree of the function fun.

    When you run the program, the output will be 0,1,0,.

    Here's how this output is generated:

    1. The first fun(--n) call reduces n from 3 to 2. This 2 is then passed to fun().

    2. The first fun(--n) call in the second level reduces n from 2 to 1. This 1 is then passed to fun().

    3. The first fun(--n) call in the third level reduces n from 1 to 0. This 0 is passed to fun().

    4. Because n is not greater than 0, this fun() call returns without doing anything.

    5. We're back to the third level, where n is printed (`0`), and then the second fun(--n) is called. Because n is already 0, it can't be reduced any further, so fun() is called with n as 0.

    6. This fun() call also returns without doing anything because n is not greater than 0.

    7. We're back to the second level, where n is printed (`1`), and then the second fun(--n) is called, with n reduced from 1 to 0. The process from steps 4-6 is repeated.

    8. We're back to the first level, where n is printed (`0`), and then the second fun(--n) is called, with n reduced from 1 to 0. The process from steps 4-6 is repeated.

    In summary, the recursive function fun(int n) prints a series of decreasing numbers, skipping every second number, down to 0. After printing 0, it stops due to the if (n > 0) condition. The sequence is created by reducing n by 1 before each fun() call and printing n between the first and second fun() calls.

    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Vishal Sharma

    MemberSep 21, 2012

    please present your code properly!
    The way you have presented it, makes the question more complicated

    #-Link-Snipped-#

    See the 5th point in above link!
    Are you sure? This action cannot be undone.
    Cancel
  • clZazy

    MemberSep 21, 2012

    I think the output should be --> 1213121
    but I don't know why you declared "proc" function which you haven't used and also the typedef in this code is useless.
    Are you sure? This action cannot be undone.
    Cancel
  • Jeffrey Arulraj

    MemberSep 22, 2012

    lots of queries why such a waste of memory here
    Are you sure? This action cannot be undone.
    Cancel
  • hare singh nayak

    MemberSep 22, 2012

    Vishal0203
    please present your code properly!
    The way you have presented it, makes the question more complicated

    #-Link-Snipped-#

    See the 5th point in above link!
    #include<stdio.h> void fun(int);
    typedef int (*pf) (int, int);
    int proc(pf, int, int);
    int main()
    {
    int a=3;
    fun(a);
    return 0;
    }
    void fun(int n)
    {

    if(n > 0)
    {
    fun(--n);
    printf("%d,", n); fun(--n);
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Vishal Sharma

    MemberSep 23, 2012

    hare singh nayak
    #include<stdio.h> void fun(int);
    typedef int (*pf) (int, int);
    int proc(pf, int, int);
    int main()
    {
    int a=3;
    fun(a);
    return 0;
    }
    void fun(int n)
    {

    if(n > 0)
    {
    fun(--n);
    printf("%d,", n); fun(--n);
    }
    }
    I don't see any use of writing the function proc as it is not defined anywhere in your program. no use of typedef too... I think, you dint post the entire program!
    still the program, wont print anything, as it is under recursion, and finally the condition fails and the program doesn't print anything..
    Are you sure? This action cannot be undone.
    Cancel
  • hare singh nayak

    MemberSep 23, 2012

    #include<stdio.h> void fun(int);
    typedef int (*pf) (int, int);
    int proc(pf, int, int);
    int main()
    {
    int a=3;
    fun(a);
    return 0;
    }
    void fun(int n)
    {

    if(n > 0)
    {
    fun(--n);
    printf("%d,", n); fun(--n);
    }

    hello friend the o/p of this program is 0,1,2,0,

    but can you explain me how .?
    Are you sure? This action cannot be undone.
    Cancel
  • grsalvi

    MemberSep 28, 2012

    #include<stdio.h>
     
     
    void fun(int);
     
    int main()
     
    {
     
    int a=3;
    fun(a);
    return 0;
     
    }
     
     
    void fun(int n)
     
    {
     
    if(n > 0)
     
    {
     
    fun(--n);
     
    printf("%d,", n);
     
    fun(--n);
     
    }
     
    return;
     
    }

    This code is seriously scary.When everthing in function fun has been executed.Instead of execution of return(bafck to main) the fun is called again...😕..well working out👍
    Are you sure? This action cannot be undone.
    Cancel
  • shameel24

    MemberFeb 1, 2016

    instead of solving above problem,solve this.. :/ i have removed every wasteless things and the o/p is 0 1 2 0. 😔 .. so pls do explain the logic

    #include<stdio.h>
    void fun(int);
    int main()
    {
    fun(3);
    }
    if(n > 0)
    {
    fun(--n);
    printf("%d ", n);
    fun(--n);
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register