Sunday, August 30, 2009

04. FizzBuzz

The FizzBuzz program took me about 10 minutes to implement. I had actually been given a FizzBuzz assignment at my internship so I already had practice. However, it took me a while to finish because I was trying to write it in a different way. In the end though, this original solution seemed the most logical.

One problem I encountered was syntax errors. I originally wrote this program with a pencil and paper, and thus did not catch some errors. I realized what a difference IDE's such as Eclipse makes.

Even though this seemed like a simple program to write, I learned that software engineering starts at the basics. One must start off knowing the fundamentals and build on that in order to become a better programmer.


/**
* FizzBuzz
* Iterates through numbers 1 to 100
* Prints "FizzBuzz" when number is multiple of 3 and 5.
* Prints "Fizz" when number is multiple of 3.
* Prints "Buzz" when number is multiple of 5.
* Prints the number otherwise.
*/
public class FizzBuzz {
  
public static void main(String[] args) {
      
for (int i = 1; i <= 100; i++) {
          
if (i % 15 == 0) {
               System.out.println
("FizzBuzz";
          
}
          
else if (i % 3 == 0) {
               System.out.println
("Fizz";
          
}
          
else if (i % 5 == 0) {
               System.out.println
("Buzz";
          
}
          
else {
               System.out.println
(i);
          
}
       }
   }

}

0 comments:

Post a Comment