Hi, starting from now I will try to post any problem sets related with algorithms. For about 1 month I have been read so many post in Quora about Software Engineer. Some of them said that under grad students has no skill coding in computer programming, even in algorithm to solve the problem. They only know theories. So from now I will try to post related with algorithm in various problem sets.  I will give the problem to solve and code it in java as the answer (solution).

FizzBuzz Question

This is kind of game for children in UK, in this game children will sit around then say each number of sequence. The numbers are multiply of three, in which case will say ‘Fizz’. The numbers are multiply of five, in which case will say ‘Buzz’. When the numbers are coming with multiply of three and five will say ‘FizzBuzz’.

Here is the question:

Given integer (N)  in loop constraint by 1<=N<=100, when N multiply of three print ‘Fizz‘. When N multiply of five print ‘Buzz‘, when N multiply of three and five print ‘FizzBuzz‘. Other than that print as usual numbers.

Let’s analize the problem! This problem very simple to solved. The instruction has been mentioned quiet clearly. The program gonna be in 2 statements (checking condition & looping). We only have to set variable called ‘N‘ with data type Integer in looping, then every loop in numbers are going to check inside condition checking (‘if’ statement), surely this going to be nested condition checking like this one.

if(condition true || false){
    if(condition true || false){
          ....//
    }
}else{
   if(condition true || false){
        ......//
   }
}

In the instruction said there will be loop as a hints. So the program don’t need any input from user, we only have to set N inside looping statement. After set looping we have to add checking condition, this part is quiet tricky. We have to think how actually when the number multiply of three and five gonna print ‘FizzBuzz‘. The numbers would be 15, 30, 45 & 100 to print ‘FizzBuzz‘ Let’s see my solution

/**
 * 06/09/16
 *
 * @author Septian Adi Wijaya
 */
public class FIzzBuzz {

            public static void main(String[] args) {
                     for (int i = 1; i <= 100; i++) {
                            if ((i % 3) == 0) {
                                 if ((i % 5) == 0) {
                                     System.out.println("FizzBuzz");
                                 }
                            System.out.println("Fizz");
                            } else if ((i % 5) == 0) {
                                       if ((i % 3) == 0) {
                                          System.out.println("FizzBuzz");
                                       }
                             System.out.println("Buzz");
                             } else {
                                     System.out.println(i);
                             }
                    }
           }
}

Let’s see in the nested checking condition, that is where algorithm used. For simply, when we are going to solve problem from problem sets. We clearly have to understand what actually the problem, after get the point of problem we arrange algorithm to solve aaaaaannnnddd *booom! magically you have been solved the problem. All the code I posted in here are open you can freely use and modify. If you have any other solution to solve the problem, let’s discuss with me 🙂

 

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