Chapter 5 exercise 5 asks to alter program 5.5 so that the user can enter the amount of triangular numbers to be

calculated and displayed. I think I have that part right, but now when the program gets to the loop to calculate the

triangular number, it's calculating the number + 1 instead. So I enter integer 4 and it calculates and displays integer 5.

I have looked over it over and over and can't see why. Any pointers appreciated. Thanks.

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])

{

@autoreleasepool {

int n, number, triangularNumber, counter, numberToCalculate;

NSLog(@"How many triangular numbers would you like to calculate?");

scanf("%i", &numberToCalculate);

for (counter = 1; counter <= numberToCalculate; ++counter)

{

NSLog(@"What triangular number do you want?");

scanf("%i", &number);

triangularNumber = 0;

for (n = 1; n <= number; ++n)

triangularNumber +=n;

NSLog(@"Triangular number %i is %i.", n, triangularNumber);

}

}

return 0;

}

2011-12-18 05:56:25.363 Ch5Ex5[3748:707] How many triangular numbers would you like to calculate?

3

2011-12-18 05:56:31.133 Ch5Ex5[3748:707] What triangular number do you want?

18

2011-12-18 05:56:34.700 Ch5Ex5[3748:707] Triangular number 19 is 171.

2011-12-18 05:56:34.701 Ch5Ex5[3748:707] What triangular number do you want?

4

2011-12-18 05:56:37.660 Ch5Ex5[3748:707] Triangular number 5 is 10.

2011-12-18 05:56:37.662 Ch5Ex5[3748:707] What triangular number do you want?

7

2011-12-18 05:56:42.195 Ch5Ex5[3748:707] Triangular number 8 is 28.