Factorial Calculator

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

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

    abcdefg12345 macrumors 6502


    Jul 10, 2013
    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

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

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

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

Share This Page