Example : Recursion


#include <iostream.h>
#include <conio.h>
void fun( int ); // prototype declaration
void main( )
{
int n;
clrscr( );
n = 5;
fun(n);
getch();
}
void fun( int k)
{
if ( k > 0)
{
cout<<k<<”\n”;
fun(k-1);
cout<<k<<”\n”;
}
}
Output :
5
4
3
2
1
1
2
3
4
5

0 comments: