Please enable JavaScript to view the comments powered by Disqus.

FizzBuzz

A few days ago I posted a link on twitter to an age-old programming problem called ‘FizzBuzz’. The task is to “Write a program that prints the integers from 1 to 100. But for multiples of three print ‘Fizz’ instead of the number and for the multiples of five print ‘Buzz’. For numbers which are multiples of both three and five print ‘FizzBuzz’”. For those learning to programme in Objective-C (like me!) this presents a problem that provides a nice challenge as it forces you to think through the logic involved, but is not something that seems overwhelming.

I’m posting my solution here together with the Xcode project. I decided to not only write a method that would solve the problem but also turn it into an iPhone app. If you have Xcode installed on your Mac then you can just download the project file and run it with your iPhone connected to the Mac and it will install on your iPhone. I have used a ‘picker’ to allow you to choose the total number of comparisons that will be made and also two numbers between 1 and 10 that serve as the ‘multiples’ numbers. The output from these comparisons is then formatted into a long string and then placed into a text view. I haven’t provided any comments in the code so that you have to figure out what I’m doing yourself! You can download the Xcode project below. Any questions then just ask.

FizzBuzz Xcode Project

@interface ViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>

@property (strong, nonatomic) IBOutlet UITextView *textOutput;
@property (strong, nonatomic) IBOutlet UIPickerView *numberPicker;
@property (strong, nonatomic) NSArray *numberArray;
@property (strong, nonatomic) NSMutableArray *totalNumberArray;
@property (strong, nonatomic) NSMutableArray *textOutputArray;
@property (strong, nonatomic) NSMutableString *textOutputString;
@property int firstNumber;
@property int secondNumber;
@property int totalNumber;
@property (strong, nonatomic) IBOutlet UIButton *button;

- (
IBAction)calculateFizzBuzz;


@implementation ViewController

- (
IBAction)calculateFizzBuzz {
[
self.textOutputString setString:@""];
[
self.textOutputArray removeAllObjects];
self.textOutput.text = @"";

// this is the main solution
for (int x = 1; x <= self.totalNumber; x++) {
if (x % self.firstNumber == 0 || x % self.secondNumber == 0) {
if (x % self.firstNumber == 0 && x % self.secondNumber > 0) {
NSLog(@"%i - Fizz", x);
NSString *output = [NSString stringWithFormat:@"%i - Fizz", x];
[
self.textOutputArray addObject:output];
}

else if (x % self.firstNumber > 0 && x % self.secondNumber == 0) {
NSLog(@"%i - Buzz", x);
NSString *output = [NSString stringWithFormat:@"%i - Buzz", x];
[
self.textOutputArray addObject:output];
}

else if (x % self.firstNumber == 0 && x % self.secondNumber == 0) {
NSLog(@"%i - Fizz and Buzz", x);
NSString *output = [NSString stringWithFormat:@"%i - Fizz and Buzz", x];
[
self.textOutputArray addObject:output];
}
}
else {
NSLog(@"%i - Nothing", x);
NSString *output = [NSString stringWithFormat:@"%i - Nothing", x];
[
self.textOutputArray addObject:output];
}
}

for (int i = 0; i < [self.textOutputArray count]; i++) {
NSString *temp = [NSString stringWithFormat:@"%@\n", [self.textOutputArray objectAtIndex:i]];
[
self.textOutputString appendString:temp];
}

self.textOutput.text = self.textOutputString;
}

blog comments powered by Disqus