Your Ad Here

Wednesday, February 25, 2009

Write C++ programs that use both recursive and non-recursive functions to find the factorial of a given integer.

int fact( int); // function declaration
void main( )
{
int no,result;
clrscr( );
cout<<"Enter the required number:"; cin>>no;
result = fact( no);
cout<<"Factorial is :" << result;
getch( );
}
int fact(int n)
{
int ft;
for( ft=1; n>=1; n--)
ft=ft*n;
return ft;
}

Output:- 4 Factorial is : 24

(a).using recursive function.


int fact( int); // function declaration
void main( )
{
int no,result;
clrscr( );
cout<<"Enter the required number:"; cin>> n0;
result = fact( no);
cout<<"Factorial :"<< result;
getch( );
}
int fact(int n)
{
int ft;
if( n==1)
return 1;
else
ft= n*fact (n-1);
return ft;
}

Output:- 4 Factorial is : 24

No comments:

Post a Comment