/*program to find the factorial of a given number*/
#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n,c;
printf("\n enter the number for which you want to find the factorial");
scanf("%d",&n);
c=fact(n);
printf("\n the factorial of the number %d is %d",n,fact);
getch();
}
int fact(int n)
{
int k;
if(n==0)
return(1);
else
k=n*fact(n-1);
return(k);
}
Chat with our AI personalities
kjhk
int factorial(int n) { int i; int f=1; for(i=2;i<=n;++i) f*=i; return f; }
Let's take the example of finding the factorial of a number (of a positive integer). The factorial of N is N * (N-1) * (N-2) * (N-3) ... * 3 * 2 *1 It is the product of all integers between that number (including that number) and 1. For example, factorial 2 = 2*1 = 2 factorial 3 = 3*2*1 = 6 factorial 4 = 4*3*2*1= 24 Now you define a recursive function Fac (N) as Fac (N) = Fac (N-1) * N, with Fac(1) predefined as 1. Thus, Fac(N-1) = Fac(N-2) * (N-1) and Fac(N-2) = Fac(N-3) * (N-2) and thus recursion takes over until such time fac(1) needs to be evaluated. We know the value of Fac(1) which is set as 1. Thus we can evaluate Factorial(N) using recursion.
#include #include using std::cin;using std::cout;using std::endl;using std::tolower;long factorial(const int& N);int main(){int N = 0; //factorial of Nchar command = 'n';do{cout > N;cout
chutia mc,bc bhosdika