Amazon.com Widgets Chapter 5 - Correction of Exercice 3
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 20, 2013, 05:30:55 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C, 4th edition
| |-+  Exercises
| | |-+  Chapter 5
| | | |-+  Chapter 5 - Correction of Exercice 3
Pages: [1]   Go Down
Print
Author Topic: Chapter 5 - Correction of Exercice 3  (Read 542 times)
microapple
Jr. Member
**
Posts: 83



WWW Email




« on: August 15, 2012, 07:33:48 PM »

Code: (Objective-C)
#import <Foundation/Foundation.h>

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

       
        int sum = 1 ;
       
        NSLog (@"Table Of Factorial");
        NSLog (@" n    Sum from 1 to n");
        NSLog (@"--   ---------------");
       
                for (int i=1; i<=10; i++) {
                    sum*=i;
                    NSLog(@"%i %9i",i,sum);
                }
       
        }
   
}
Logged

MacD
Newbie
*
Posts: 20






« Reply #1 on: December 29, 2012, 09:49:19 PM »

Using nested for loop. microapple implementation is simpler less code.

Code: (Objective-C)
/*
 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
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.