# Natural log approach for calculating factorial for large number

Hello there everyone. I am trying to solve this problem but I am getting the wrong answer

APPROACH - Natural Log Factorial Algorithm

Here is my code, for which I have checked factorial of 1,100,51,27… and it was correct output but when I submit solution I am getting the Wrong Answer can anyone please suggest me what I am doing wrong

``````#include <bits/stdc++.h>
#define ll long long int
using namespace std;

void gogo(){
int n;
cin >> n;
if(n==1){
cout << 1 << endl;
return;
}
long double s=0;
for(int i = 1 ; i <= n ; i++)
s += log(i);
string r = to_string(round(exp(s)));

for(int i = 0 ; i < r.length() ; i++){
if(r[i] == '.')
break;
else
cout<<r[i];
}
cout<<endl;
}

int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int T;
cin >> T;
while (T--) {
gogo();
}
return 0;
}
``````

Floating point inaccuracy will make your answer incorrect, you also cannot just randomly decide to round all the logs. The GFGs code is wrong, the natural log method is mathematically sound but will only give an approximation. For instance, your value of 100! has no zeroes at the end, which is just wrong. The tutorial can be found here.

1 Like

thank you!