calculating factorials with FOR LOOP

The following is a sample program out of my book. The program's description is lousy and I cannot figure out what exactly how it works.. Could someone please help me understand it. I understand how for loops work, but this program has TWO for loops in it, and it's confusing..

Thanks

#include <iostream>
using namespace std;


const int ArSize = 16;
int main()
{
double factorials[ArSize];
factorials[1] = factorials[2] = 1.0;
int i;

for (i = 2; i < ArSize; i++)
factorials[i] = i * factorials[i-1];

for (i = 0; i < ArSize; i++)
cout << i << " ! = " << factorials[i] << "\n";

return 0:

}


OUTPUT:

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3.6288e+006
11! = 3.99168e+007
12! = 4.79002e+008
13! = 6.22702e+009
14! = 8.71783e+010
15! = 1.30767e+012

The first for loop calculates the factorials.

The second prints them out.
That makes sense! I can't believe that's all it took to get me to understand it.


I don't know what I would do with this C Plus Plus Forum, it has helped me get past many walls. There aren't any classes at community colleges around here that offers classes on C++, so I got a text book and have been trying to teach myself..


thanks :)
Topic archived. No new replies allowed.