Write a C Program to Find Factorial of a number using Recursive Function
Source Code
#include<stdio.h>
long fact(int);
void main()
{
int num;
long fact_value;
printf("Enter a number to find factorial: ");
scanf("%d",&num);
fact_value=fact(num);
printf("Factorial of %d is %ld",num,fact_value);
}
long fact(int n)
{
if(n>=1)
return(n*fact(n-1));
else
return(1);
}
Output
Enter a number to find factorial: 5
Factorial of 5 is 120
Enter a number to find factorial: 8
Factorial of 8 is 40320
No comments:
Post a Comment