Exercise 5 - Functions
Exercise 5 - Functions
Exercise 5 Functions
Write a C program to find the factorial of a given integer number using
both recursive and non-recursive functions.
Objective:
The objective of this program is to:
Calculate the factorial of integer number using recursive and non-recursive
functions.
Program Code:
Non Recursive:
#include<stdio.h>
#include<conio.h>
void main()
{
intn,i,f;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
f=1;
for(i=1;i<=n;i++)
f=f*i;
printf("Factorial of %d = %d",n,f);
getch();
}
Expected Output:
Enter the number: 4
Factorial of 4 = 24
Recursive:
longint fact(int n)
main( )
{
int n;
clrscr( );
printf(“\n enter an integer no:”);
scanf(“%d”,&n);
printf(“ factorial of %d = %d \n”, n, fact(n));
getch( );
}
longint fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
Expected Output
Recursive
enteraninteger no: 9
factorial of 9 = 362880