c program for factorial when 5different numbers are given
Answers
Answer:
Factorial of a non-negative integer, is multiplication of all integers smaller than or equal to n. For example factorial of 6 is 6*5*4*3*2*1 which is 720.
Recursive Solution:
Factorial can be calculated using following recursive formula.
n! = n * (n-1)!
n! = 1 if n = 0 or n = 1
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
Following is implementation of factorial.
// C++ program to find factorial of given number
#include<iostream>
using namespace std;
// function to find factorial of given number
unsigned int factorial(unsigned int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
// Driver code
int main()
{
int num = 5;
cout << "Factorial of " << num << " is " << factorial(num) << endl;
return 0;
}
// This code is contributed by Shivi_Aggarwal
Output:
Factorial of 5 is 120