Sunday, August 30, 2009

FizzBuzzed

The FizzBuzz program is a simple program some interviewers use to test basic programming skills of applicants. The FizzBuzz program counts from 1 to 100 and prints out “Fizz” if the number is divisible by 3, “Buzz” if the number is divisible by 5, “FizzBuzz” if the number is divisible by both 3 and 5, and the number itself otherwise. The code for the FizzBuzz program is shown below.

public class fizzbuzz {

  
public static void main(String[] args) {
      
      
// Loop 1 through 100
      
for(int i = 1; i <= 100; i++){
          
          
// Display FizzBuzz if i is divisible by 3 and 5
          
if(i % 15 == 0){
               System.out.println
("FizzBuzz");
          
}
          
          
// Display Fizz if i is divisible by 3
          
else if(i % 3 == 0){
               System.out.println
("Fizz");
          
}
          
          
// Display Buzz if i is divisible by 5
          
else if(i % 5 == 0){
               System.out.println
("Buzz");
          
}
          
          
// Display number if i is not divisible by 3 or 5
          
else{
               System.out.println
(i);
          
}
       }
   }
}  


It took me about 5 minutes to implement this program and verify the output was accurate. I didn’t run into any real problems, except that I couldn’t remember some of the syntax. Luckily, the Eclipse IDE auto-complete assisted with this problem. This experience helped me realize that programming, like other skills, need to be practiced to keep them sharp. Thankfully, IDEs, like Eclipse, exist to make life easier. Through the programming assignments in this class, I hope to sharpen my programming skills and become a more efficient programmer.

No comments:

Post a Comment