What will be the output of this program ?

please give the complete detailed solution of this program ..
#include 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 

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.

Replies

  • Vishal Sharma
    Vishal Sharma
    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!
  • clZazy
    clZazy
    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.
  • Jeffrey Arulraj
    Jeffrey Arulraj
    lots of queries why such a waste of memory here
  • hare singh nayak
    hare singh nayak
    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 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);
    }
    }
  • Vishal Sharma
    Vishal Sharma
    hare singh nayak
    #include 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..
  • hare singh nayak
    hare singh nayak
    #include 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 .?
  • grsalvi
    grsalvi
    #include
     
     
    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👍
  • shameel24
    shameel24
    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
    void fun(int);
    int main()
    {
    fun(3);
    }
    if(n > 0)
    {
    fun(--n);
    printf("%d ", n);
    fun(--n);
    }
    }

You are reading an archived discussion.

Related Posts

Can we build the buildings in the air with the help of gravity force? if possible..how,if not why?
I am basically a school teacher and helps all rural students in my area about good educational websites for their academic work and projects.
Hello everyone, I am newly joined 2nd year engineer student to electronics field through diploma background, I want your guideline I want to know being a engineer what skills and...
can anybdy tell about WiMC,,wireless mobile computing?????????????
My Android doesn't supports Flashplayer,so i rooted it and then installed flashplayer 11.1 in phone. After restart I tried setting up FP in Opera. It did not detected. So I...