The FizzBuzz problem is a standard programming exercise. The programmer must write a programme that prints out the numbers from 1 to 100 replacing multiples of 3 with the word Fizz, multiples of 5 with the word Buzz and multiples of 3 and 5 with the word FizzBuzz.

A simple version of the programme could be this:

/************************************************************************* * Author: MazzyK * Description: a programme that prints out the numbers from 1 to 100 replacing multiples of 3 with the word Fizz, multiples of 5 with the word Buzz and multiples of 3 and 5 with the word FizzBuzz. *************************************************************************/ public class fizzbuzz { public static void main(String[] args) { int[] Numbers = new int[100]; int i,j; j=1; for (i=0;i<100;i++) { Numbers[i] = j; j++; } for (i=0; i<100; i++) { if ((Numbers[i]%3 ==0)&&(Numbers[i]%5 ==0)) { System.out.println("FizzBuzz"); }else if ((Numbers[i]%3 ==0) && (Numbers[i]%5 !=0)) { System.out.println ("Fizz"); }else if ((Numbers[i]%5 ==0) && (Numbers[i]%3 !=0)) { System.out.println("Buzz"); }else { System.out.println (Numbers[i]); } } } }

However, if the programmer is then asked to change the array size or change the multiples conditions to other numbers they would have to be changed in individual places in the programme. In addition to this, it is always preferable to keep the main function small. Taking all of this into account the following solution is more sophisticated (though by no means is it definitive):

public class fizzbuzz { public static void main(String[] args) { int ArraySize = 100; //array size - change here to change throughout programme int Fizz = 3; //divisor 1 - change here to change divisor throughout programme int Buzz = 5; //divisor 2 - change here to change divisor throughout programme int[] Numbers = new int[ArraySize]; //declare and initialise array fillArray(Numbers,ArraySize); //populate array with numbers from 1 to ArraySize printArray(Numbers, ArraySize, Fizz, Buzz); //print array contents using method conditions } //check a number is a multiple of another number public static boolean modZero (int num, int divisor) { if (num%divisor ==0) { return true; }else { return false; } } //a method that populates an array with the numbers 1 to size public static void fillArray (int Array[], int size) { int j=1; for (int i=0;i<size;i++) { Array[i] = j; j++; } } //print array contents public static void printArray (int Array[], int size, int divsr1, int divsr2) { for (int i=0; i<size; i++) { if ((modZero(Array[i],divsr1))&&(modZero(Array[i],divsr2))){ //if array item a multiple of divsr1 and divsr2 print FizzBuzz System.out.println("FizzBuzz"); }else if ((modZero(Array[i],divsr1))&&!(modZero(Array[i],divsr2))) { //if array item is a multiple of divsr1 but not divsr2 then print Fizz System.out.println ("Fizz"); }else if ((modZero(Array[i],divsr1))&&!(modZero(Array[i],divsr2))) { //if array item is a multiple of divsr2 but not divsr1 then print Buzz System.out.println("Buzz"); }else { System.out.println (Array[i]); //print array item } } } }

Tom Dalling really delves into the problem and offers some great solutions which are not programming-specific but are rather offered up as pseudocode here.

Don’t get too discouraged his article is titled *FizzBuzz in too much detail*

And when it comes to trying things out remember …