0

This is my code to find factorial inverse

double num;
cin>>num;
double i=1,fact=1;
for(i=1;i>0;++i)
{
    fact*=i;
    if(fact==num)
    {
        cout<<i;
        break;
    }}

But when i put the factorial of 28 the loop became infinite. When i output fact to check the loop, the answer was output among other factorial numbers but the loop kept going after the right factorial of 28. It works up to 27! .

Pine
  • 1
  • What exactly do you mean by factorial inverse ,provide a sample input and output which you are expecting – Rishabh Ankit Jun 11 '20 at 08:19
  • 51090942171709440000 would output 21 as 51090942171709440000 is the factorial of 21, 10888869450418352160768000000 would output 27, but it won't output 28 when i input 304888344611713801550158626816 – Pine Jun 11 '20 at 09:45
  • Beacuse the factorial of 28 is out of range of double data type – Rishabh Ankit Jun 11 '20 at 13:08
  • But isn't double's range is 1.8e+308, and the factorial of 28 is 3.04888e+29 which is clearly in range? – Pine Jun 12 '20 at 02:42
  • Yeah but beyond 20 significant digits double doesn't give exact results – Rishabh Ankit Jun 12 '20 at 03:35
  • Look this , It will help : [Double precision](https://stackoverflow.com/questions/14637621/c-calculating-more-precise-than-double-or-long-double) – Rishabh Ankit Jun 12 '20 at 03:40

0 Answers0