- کد: انتخاب همهٔ
#import <Foundation/Foundation.h>
@interface Factorial : NSObject
{
long numToFact;
long factResult;
}
-( long ) rFactorial: x;
-( void ) displayFactorial;
@end
@implementation Factorial
-( long ) rFactorial: x
{
factResult = 1;
numToFact = x;
for ( int i = x; i > 1; i-- )
factResult *= i;
}
-( void ) displayFactorial
{
NSLog(@" the factorial of %i is : %i ", numToFact, factResult );
}
@end
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Factorial *fact = [ [ Factorial alloc] init];
for ( int i = 1; i <= 10; i ++ )
{
[ fact rFactorial: i ];
[ fact displayFactorial ];
}
[pool drain];
return 0;
}
اگر warning اخطار دیدید مشکل نداره
- خروجی
2010-05-01 16:40:17.150 oopFactorial[2006:903] the factorial of 1 is : 1
2010-05-01 16:40:17.153 oopFactorial[2006:903] the factorial of 2 is : 2
2010-05-01 16:40:17.153 oopFactorial[2006:903] the factorial of 3 is : 6
2010-05-01 16:40:17.154 oopFactorial[2006:903] the factorial of 4 is : 24
2010-05-01 16:40:17.154 oopFactorial[2006:903] the factorial of 5 is : 120
2010-05-01 16:40:17.155 oopFactorial[2006:903] the factorial of 6 is : 720
2010-05-01 16:40:17.155 oopFactorial[2006:903] the factorial of 7 is : 5040
2010-05-01 16:40:17.156 oopFactorial[2006:903] the factorial of 8 is : 40320
2010-05-01 16:40:17.156 oopFactorial[2006:903] the factorial of 9 is : 362880
2010-05-01 16:40:17.157 oopFactorial[2006:903] the factorial of 10 is : 3628800
any problem mail me at : kalakbarghi@gmail.com