0% found this document useful (0 votes)
34 views2 pages

I Hope This Explanation Would Clarify The Cause of The Given Problem For The Question of

The document discusses a recursive function that prints numbers in descending order. It contains: 1) An example C program with a recursive function fun() that takes an integer as a parameter, prints the integer, and calls itself recursively while decrementing the integer. 2) An explanation of how the recursion works, with a tree representation showing how the problem is divided into subproblems at each step. 3) The output of the example program is explained to be the numbers in descending order due to an inorder traversal of the recursion tree.

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views2 pages

I Hope This Explanation Would Clarify The Cause of The Given Problem For The Question of

The document discusses a recursive function that prints numbers in descending order. It contains: 1) An example C program with a recursive function fun() that takes an integer as a parameter, prints the integer, and calls itself recursively while decrementing the integer. 2) An explanation of how the recursion works, with a tree representation showing how the problem is divided into subproblems at each step. 3) The output of the example program is explained to be the numbers in descending order due to an inorder traversal of the recursion tree.

Uploaded by

Abhishek Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

I hope this explanation would clarify the cause of the given problem

for the question of


void fun(int);
int main()
{
int a=3;
fun(a);
printf("\n");
return(0);
}
void fun(int n)
{
if(n>0)
{
fun(--n);
printf("%d",n);
fun(--n);
}
}
see it is a simple problem of reursion
ever! time t"e sub problem is divided into n-# left subtree and n-$ ri%"t subtree ,
i&e&
n
n-# n-$
n-3 n-' n-( n-)
so on&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
no* furt"er in t"e above problem sine n=3 so and sta+ %ets fill form n-# sine it is deremented
first t"en assi%ned so t"e reursion tree *ould be ,
$ no* t"e tree traversal *ould be
in t"e inorder fais"on i&e& ,eft ,
root and ri%"t so t"e desired output is --
# 0 0(leftmost) t"en # t"e root t"en sine no ri%"t
node so move to root $ t"en t"e ri%"t part
i&e& 0 t"is li+e t"e sta+ %ets empt! and t"e
0 output is 0#$0 ,
but for this question
void fun(int);
int main()
{
int a=3;
fun(a);
printf("\n");
return(0);
}
void fun(int n)
{
if(n>0)
{
fun(n-#);
printf("%d",n);
fun(n-#);
}
}
ever! time t"e sub problem is divided into n-# left subtree and n-# ri%"t subtree , and so on
i&e&
n
n-# n-#

n-$ n-$ n-$ n-$
so t"e proposed reursion tree of t"e above pro%ram *ould be
3 (root)
$(left) $(ri%"t)

# # # #
and t"e same inorder traversal #$#3#$#
./ -- 0.123145 56708 94870
:2; <68=0>68
?4.32;4 -- ab"is"e+$@&*ordpress&om

You might also like