How to calculate factorial for big numbers with long double in C++

1 Answer

0 votes
#include <iostream>
#include <iomanip>

using std::cout;
using std::endl;

int main()
{
	long double n = 1.0, factorial = 1.0;

	cout.precision(0);           
	cout.setf(std::ios::left, std::ios::fixed);
  
	 for (int i = 0; i < 21; i++) {
		factorial *= n;
		n++;
		cout.width(13);            
		cout << factorial << endl;
	}


	return 0;
}


/*
run:

1
2
6
24
120
720
5040
40320
362880
3.6288e+06
3.99168e+07
4.79002e+08
6.22702e+09
8.71783e+10
1.30767e+12
2.09228e+13
3.55687e+14
6.40237e+15
1.21645e+17
2.4329e+18
5.10909e+19

*/

 



answered May 26, 2018 by avibootz

Related questions

2 answers 217 views
1 answer 188 views
1 answer 182 views
2 answers 136 views
1 answer 189 views
2 answers 200 views
...