Q) Andrew was attempting a mathematics test where he needed to solve problems with factorials. One such problem had an answer which equaled 100! ,He wondered what would this number look like. He tried to calculate 100! On his scientific calculator but failed to get a correct answer. Can you write a code to help Andrew calculate factorials of such large numbers?
Input Format
a single lined integer N
Constraints
1 < = N < = 500
Output Format
Print the factorial of N
Sample Input
5
Sample Output
120
Explanation
for factorial of 5 we have 5 x 4 x 3 x 2 x 1 = 120
Code implemented by me :-
#include
using namespace std;
long unsigned int rec ( int x )
{
long unsigned int f ;
if ( x == 1 || x == 0 )
return ( 1 ) ;
else
{
f = x * rec ( x - 1 ) ;
return ( f ) ;
}
}
int main( )
{
long unsigned int a, fact ;
// cout << "Enter any number " ;
cin >> a;
fact = rec ( a ) ;
// cout << "Value: "<<fact;
cout << fact;
return 0;
}
In this above code, I am getting the correct answer upto some extent but not for all TEST CASES.
Can you Please figure out the problem and update my code.
Else, Provide me the code for the above Question.
Thank You