In this program, we will find the factorial of a given number. Recursion is the process of defining a problem n terms of itself. A recursive function is a function that calls itself during its execution. The process may repeat several times, outputting the result and the end of each iteration. The function fact() in program uses recursion to find factorial of a number. The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 5 (denoted as 5!) is 1*2*3*4*5 = 120. Factorial is not defined for negative numbers and the factorial of zero is one, 0! = 1 .
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n;
printf("Enter the number: ");
scanf("%d", &n);
printf("Factorial %d! = %d", n, fact(n));
getch();
}
int fact(int no)
{
if(no == 0)
return 1;
else
return no*fact(no-1);
}
Enter the number: 5
Factorial 5! = 120