Keyboard Input

Program 5.2 calculates the 200th triangular number, and nothing more. What if you want to calculate the 50th or the 100th triangular number instead? Well, in that case, you have to change the program so that the for loop is executed the correct number of times. You also have to change the NSLog statement to display the correct message.

An easier solution might be to somehow have the program ask you which triangular number you want to calculate. Then, after you had given your answer, the program could calculate the desired triangular number. You can effect such a solution by using a routine called scanf. The scanf routine is similar in concept to the NSLog routine. Whereas the NSLog routine is used to display values, the purpose ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.