Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
October 19, 2017, 04:56:59 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Chapter 5 Exercise 3 Solution  (Read 7399 times)
Jian Zhou
Newbie
*
Posts: 20



Email
« on: October 01, 2013, 05:51:08 AM »


#import <Foundation/Foundation.h>

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

    @autoreleasepool {
       
        int i,j;
        int factorial=1;
       
        for(j=1;j<=10;j++)
        {
            for(i=1;i<=j;i++)
            {
                factorial*=i;
            }
           
            NSLog(@"%i_________%i",j,factorial);
           
            factorial=1;
        }
       
    }
    return 0;
}
Logged
bertobettencourt
Newbie
*
Posts: 45


Email
« Reply #1 on: October 08, 2013, 06:58:34 PM »

// A different approach, with simpler coding

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

    @autoreleasepool {
       
        int number, factorial = 1;
       
        for (number = 1; number <= 10 ; number++) {
            factorial *= number;
            NSLog(@"Displaying the table of the first 10 factorials: %i -> %i", number, factorial);
        }
       
    }
    return 0;
}
Logged
plaisance
Newbie
*
Posts: 4


« Reply #2 on: December 22, 2013, 06:48:51 PM »

since everyone did factorial 1 to 10, which is the challenge, i decided to go off the menu and show how to do factorial 0 to 10, which requires a little more code to accomplish.

Code: (Objective-C)
 int x;
        
        for (x=0; x<=10; x++) {
            int y =1, n = x;
            while (n)
            y *= n--;
            
          NSLog(@"%2d, %d",x,y);
        }
« Last Edit: December 22, 2013, 07:03:23 PM by plaisance » Logged
TheTopBloke
Newbie
*
Posts: 39


« Reply #3 on: April 10, 2014, 01:09:11 AM »

since everyone did factorial 1 to 10, which is the challenge, i decided to go off the menu and show how to do factorial 0 to 10, which requires a little more code to accomplish.

Code: (Objective-C)
 int x;
        
        for (x=0; x<=10; x++) {
            int y =1, n = x;
            while (n)
            y *= n--;
            
          NSLog(@"%2d, %d",x,y);
        }

I like that style. I'm going to check this out and see what the results are. In the meantime, I did similar to what OP did, but using "the first ten" ie. 0-9. I had to look to the forums and realized that my problems were solved after I took the NSLog out of the nested for loop and added the factorial=1; to reset it each time.

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

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

    @autoreleasepool
    {
       
        int n, j;
        int factorial=1;
       
        for (n=0; n<10; n++)
        {
            for (j=1; j<=n; j++)
            {
                factorial *=j;
            }
           
            NSLog(@"Factor of %i is %i", n, factorial);
            factorial=1;
        }
    }
    return 0;
}

OUTPUT

2014-04-10 01:03:42.815 Ch5Ex3[15213:303] Factor of 0 is 1
2014-04-10 01:03:42.817 Ch5Ex3[15213:303] Factor of 1 is 1
2014-04-10 01:03:42.818 Ch5Ex3[15213:303] Factor of 2 is 2
2014-04-10 01:03:42.818 Ch5Ex3[15213:303] Factor of 3 is 6
2014-04-10 01:03:42.819 Ch5Ex3[15213:303] Factor of 4 is 24
2014-04-10 01:03:42.820 Ch5Ex3[15213:303] Factor of 5 is 120
2014-04-10 01:03:42.820 Ch5Ex3[15213:303] Factor of 6 is 720
2014-04-10 01:03:42.821 Ch5Ex3[15213:303] Factor of 7 is 5040
2014-04-10 01:03:42.821 Ch5Ex3[15213:303] Factor of 8 is 40320
2014-04-10 01:03:42.822 Ch5Ex3[15213:303] Factor of 9 is 362880
Program ended with exit code: 0
Logged
Jimmy Pete
Newbie
*
Posts: 3


« Reply #4 on: July 08, 2014, 05:25:48 PM »

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

int main(int argc, char *argv[])
{
    @autoreleasepool {
       
        double factorial = 10;
        int n = factorial;
        int starting = factorial;
       
        for (; n > 1; n--) {
            factorial *= n - 1;
        }
        NSLog(@"%i! = %le", starting, factorial);
    }
    return 0;
}

Result: 10! = 3.628800e+06
Logged
mederafael
Newbie
*
Posts: 2


Email
« Reply #5 on: February 04, 2016, 05:21:25 AM »

Recursiva !!! Grin

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

@interface Fatorial : NSObject

- (int) fatorial: (int) fator;

@end

File Factorial.m

Code: (Objective-C)

#import "Fatorial.h"

@implementation Fatorial

- (int) fatorial: (int) fator
{

    if (fator == 0 || fator == -1 ) {
        return 1;
    }

    return fator * [self fatorial: (abs(fator) - 1)];
}

@end

File Main

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Fatorial *fatorial = [Fatorial new];
        NSLog(@"Fatorial de %i = %i", 5, [fatorial fatorial: 5 ]);
        NSLog(@"Fatorial de %i = %i", -5, [fatorial fatorial: -5 ]);
    }
    return 0;
}
« Last Edit: February 04, 2016, 05:23:14 AM by mederafael » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.