Question -
Given any Integer number, you have to find the factorial of that number.
Example -
Input : num = 5
Output : Factorial of 5 is : 120
Approach -
as we know the formula of factorial.
n!=1*2*3*.......*(n-1)
Implementation -
// to find factorial of any integer number
#include<iostream>
using namespace std;
int factorial(int num)
{
int fact=1;
for(int i=1;i<=num;i++){
fact*=i;
}
return fact;
}
int main()
{
int num;
cout<<"Enter the number : ";cin>>num;
int fact=factorial(num);
cout<<"Factorial of "<<num<<" is : "<<fact<<endl;
return 0;
}
Time complixity - O(n)
Comments
Post a Comment