Using nested for loop. microapple implementation is simpler less code.
/*
Programming in Objective-C, 4th edition
Chapter 5 Exercise 3
*/
# import <Foundation/Foundation.h>
int main (int argc, char * argv[] )
{
@autoreleasepool {
NSLog (@"n \t\t factorial");
NSLog (@"--------------------------");
for (int i = 1; i <= 10; i++)
{
int factorial = 1;
for (int j = i; j >= 1; j--)
{
factorial *= j;
}
NSLog (@"%i \t\t %i", i, factorial);
}
}
return 0;
}
Output:
n factorial
-------------------------------
1 1
2 2
3 6
4 24
5 120
6 720
7 5040
8 40320
9 362880
10 3628800