下面是对于三角形排列示例:
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
int n = 0;
int number = 0;
int triangularNumber = 0;
NSLog(@"What triangular number do you want?");
scanf("%i", &number); //C语言风格的scanf函数
for (n = 1; n <= number; ++n) {
triangularNumber += n;
}
NSLog(@"Triangular number %i is %i.\n", number, triangularNumber);
}
return 0;
}
程序的输出结果如下:
2014-04-07 10:38:29.961 Object-Test[527:303] What triangular number do you want?
100 //我这里输入100
2014-04-07 10:38:34.253 Object-Test[527:303] Triangular number 100 is 5050.
Program ended with exit code: 0
黑色字体就是输出结果。