Thursday, February 5, 2009

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

(i). to find the factorial of a given integer
(a).without using recursive function
.


#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int); // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}
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.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int fact( int); // function declaration
int main( )
{
int no,result;
clrscr( );
printf("Enter the required number:");
scanf("%d", &no);
result = fact( no);
printf("\n %d Factorial is : %d", no, result);
getch( );
return 0;
}

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