#include <iostream>
using namespace std;
int main()
{
int i,fact=1,number;
cout<<"Enter any Number: ";
cin>>number;
for(i=1;i<=number;i++){
fact=fact*i;
}
cout<<"Factorial of " <<number<<" is: "<<fact<<endl;
return 0;
}
#include <cmath>
int fact(int n){
return std::tgamma(n + 1);
}
// for n = 5 -> 5 * 4 * 3 * 2 = 120
//tgamma performas factorial with n - 1 -> hence we use n + 1
/*#include<bits/stdc++.h>
using namespace std;
iterative solution:
int fact (int n, int k)
{
if(n==0) return 1;
return fact(n-1,k*n);
}
int main()
{
int n,k=1;
cin>>n;
int ans=fact(n,k);
cout<<ans<<endl;
}*/
//recursive solution.
#include<bits/stdc++.h>
using namespace std;
int fact(int n)
{
if(n==0)return 1;
return n*fact(n-1);
}
int main(){
int n;
cin>>n;
cout<<fact(n)<<endl;
}
int factorial(int n)
{
return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
#include<iostream>
using namespace std;
int factorial(int x){
if(x==0){ // Base-case ( VERY IMPORTANT)
return(1);
}
else{
return(x*(factorial(x-1))) ;
}
}
int main(){
int no = 5;
cout << factorial(no) ; // 120
}
#include<iostream>
using namespace std;
int main(){
int no = 5;
int factorial = 1;
for(int a=no;a>0;--a){ //note: decrement(--)
factorial *= a ; // note(*=)
}
cout << factorial ; // 120
}