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

Posted by Tushar Bedekar

(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

(b).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;
}

0 comments:

Post a Comment

back to top