Monday, August 30, 2010

Fizz Buzz Program

When I first read about writing the Fizz Buzz program, I got worried. I never wrote a Java program for almost 2 years. It took me about 45 minutes from the moment I opened Eclipse, writing the comments and getting the write results. Most of the time though were spent figuring out on how to get started on writing a java program. I had to search for my previous java assignments just to get an idea on how I used to write it. 

The program below is what I came up with. It's not very robust and as good as other programmer would do it, but it works. 

It was fun writing the code for this program mainly because it was very simple and I'm not expecting the same for the coming programs that need to be coded.  

/**
* Fizz Buzz
* A program that loops from 1 to 100. It displays Fizz if the number is divisible by 3,
* Buzz if by 5, Fizz Buzz if both and the number itself if not divisible by 3, 5 or both.
* @author Emerson Tabucol
* @date August 30, 2010
*/

import java.*;

public class FizzBuzz {
  
  
public static void main(string[] args){
      
      
for (int i = 1; i <= 100; i++){
      
          
if (((i%3) == 0) &amp;&amp; ((i%5)== 0))  { //if no. is divisible by 3 and 5
              
System.out.print("FizzBuzz"); //print Fizzbuzz
              
System.out.println(); //newline
          
          
}else if ((i%3) == 0){ //if no. is divisible by 3
              
System.out.print("Fizz"); //print Fizz
              
System.out.println(); //newline
              
          
}else if ((i%5) == 0) { //if no. is divisible by 5
              
System.out.print("Buzz"); //print Buzz
              
System.out.println(); //newline
              
          
}else{
               System.out.print
(i); //print the number
              
System.out.println(); //newline
          
}
       }
      
   }
//end main
}//end class

No comments:

Post a Comment