Factorial Calculator

Discussion in 'Mac Programming' started by abcdefg12345, Apr 26, 2014.

  1. abcdefg12345, Apr 26, 2014
    Last edited: Apr 26, 2014

    abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    Im trying to make a button that calculates the factorial of the number interred into a textfield,

    is there a way to do this with Xcode, the ! sign docent work is there some kind of formula i can use to find the factorial of a number using Xcode

    thats what i done but its not working


    .h
    Code:
    @property (weak) IBOutlet NSTextField *Field;
    - (IBAction)Calculate:(id)sender;
    
    .m
    Code:
    - (IBAction)Calculate:(id)sender
    {
        float result = ([_Field floatValue] * ([_Field floatValue]-1)*[_Field floatValue]);
        [_Field setFloatValue:result];
    }
     
  2. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #2
    ! is a logical negation operator in C and therefor Obj-C. You need to create a loop that multiplies all values together. Something like;

    Code:
    result = 1
    
    for i in {1 .. n}
        result = result * i
    
     

Share This Page