Source code:
//Write a C program to calculate factorial of a number using recursion.
#include<stdio.h>
int factorial(int n)
{
if(n==0)
return 1;
else
return (factorial(n-1)*n);
}
int main()
{
int num,f;
printf("Enter a number: ");
scanf("%d",&num);/*Enter the number whose factorial to be found*/
f=factorial(num);
/*Write the factorial(num) function which calculates the function in recursive method and returns f */
printf("Factorial of %d = %d",num,f);
return 0;
}
Output:
No comments:
Post a Comment