FizzBuzz

Posted: 29 October 2012 in Java

I was reading this article http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html and I created some code. I laughed. I made a quick class. I could have used a switch instead.

package picquet.blog.samples;

/**
*
* @author laurentp
*/
public class FizzBuzz {

/*
* Write a program that prints the numbers 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”.
*/
public static void main(String[] args) {

for (int aNumber = 1; aNumber <= 100; aNumber++) {
if ((aNumber % 3 == 0) && (aNumber % 5 == 0)) {
System.out.println(“FizzBuzz”);
} else if ((aNumber % 3 == 0)) {
System.out.println(“Fizz”);
} else if ((aNumber % 5 == 0)) {
System.out.println(“Buzz”);
} else {
System.out.println(aNumber);
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s