#include <iostream>
using namespace std;
double fact(int n){
if(n<=100 && n>=1){
double factorial = 1;
for (int i = 2; i<=n; i++)
{
factorial*=i;
}
return factorial;
}
else
{
return 0;
}
}
int main() {
int n,current;
cin>>n;
double arr[n];
if(n<=100 && n>=1){
for(int i = 0; i < n; i++){
cin>>arr[i];
}
}
else
{
return 0;
}
for(int i = 0; i < n; i++){
current = arr[i];
cout<<fact(current)<<endl;
}
return 0;
}
This should explain the problem:
You can also check this tutorial:
I know that a long long int data type’s variable can not store the value of 100!. That’s why I am using double data type. You can check it manually that the code works fine with my input 100
How is 100! viz.,
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
Equal to 9.33262e+157
?
Please try to search on google to find out the value of 100!. Then you will understand the value you gave & mine are both equal.
LMAO, how can 100! end with a digit other than 0?
Edit: Your submission will only be accepted if you print the expected answer (Not in Scientific Notation).
For N = 100, your code must output
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
not 9.33262e+157
If the number is like this → 1.524157875019e+16
This doesn’t mean that the number is ending with 16. It means there are total 16 digits after the decimal point.
It means : 1.524157875019e+16 = 15,241,578,750,190,000
Similarly : 8.10000007371e-9 = 0.00000000810000007371
• +
denotes positive power of 10 and -
denotes negative power of 10.
Hence, 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
and 9.33262e+157
, both are same.
You doubted Google’s Calculator
Okay, my perception of e + K in Scientific Notation was wrong. But this is not true.
Scientific notation is meant to represent large numbers (to show how big they are) in a more convenient way. Which means, 9.33262e + 157 is an approximation of 100!, whereas
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
is the true value.
PS: Note that For your Submission to get accepted, you should print the true value, not an approximation.
I think this is the problem with this particular question. Because later I found a question exactly same with this. I applied the same code there & get the correct answer.
Here is the question link.
And here is the solution treated as correct answer.
for(int i=0; i<t; i++){ // t --> test cases
int n;
cin>>n;
int fact=1;
while(n!=0){
fact*=n;
n--;
}
cout<<fact<<endl;
}
//when 'fact' is declared as 'int', for n= 15, OUTPUT: 2004310016 (10 digits)
//when I declare 'fact' as 'double', OUTPUT: 1.30767e+12 (13 digits)
//Both these solutions are getting accepted.
Exactly this question has no issue with the code.