How to solve FizzBuzz inwards Java?

FizzBuzz is i of the most oft asked questions on programming interviews together with used to filter programmers who can't program. It looks extremely unproblematic but it's tricky for those programmers or coder who combat to construction their code. Fizzbuzz work disputation is real simple, write a computer program which provide "fizz" if the discover is a multiplier of 3, provide "buzz" if its multiplier of 5 together with provide "fizzbuzz" if the discover is divisible past times both 3 together with 5. If the discover is non divisible past times either 3 or 5 therefore it should but provide the discover itself. You tin regard null is fancy when it comes to thinking almost the solution, but when you lot start coding, you lot volition regard a work amongst structuring your code, peculiarly if else blocks.

In this article, nosotros volition acquire through a span of solution to empathize the crux of this problem. If you lot are learning Java 8, therefore you lot tin likewise solve this work using lambda appear together with streams, equally shown here.

Btw, If you lot are preparing for programming project interviews together with looking for some coding problems to laid upwards well, therefore I volition recommend you lot to acquire through problems given inward the book, Cracking the Coding Interview. It contains to a greater extent than or less 150 programming questions together with their solutions amongst a proficient explanation.


First solution of FizzBuzz Problem

Many programmers come upwards up amongst the next solution when commencement faced amongst Fizzbuzz work :
 public static String fizzBuzz(int number) {         if (number % 3 == 0) {             if (number % 5 == 0) {                 return "fizzbuzz";             } else {                 return "fizz";             }         } else if (number % 5 == 0) {             return "buzz";         }         return String.valueOf(number);     }


If you lot await at this solution closely you lot volition honour that the construction of the if statements is non good, together with at that topographic point are 2 tests for the same status i.e. 2 if block to cheque if the discover is divisible past times 5. The work is the code is non proficient but you lot should laissez passer on the create goodness of the doubtfulness to the programmer because fifty-fifty though a examination of 5 is written twice they are inward unlike branches, together with entirely i of them volition execute, but I grip the code is non clean.



Second solution of FizzBuzz

They fundamental of coming amongst perfect solution is coming upwards amongst an approach to create the examination for divisible past times both 3 together with 5 first. This brings to a greater extent than readability without duplication, equally shown here
public static String fizzBuzz2(int number) {          if (number % 15 == 0) {             return "fizzbuzz";         } else if (number % 5 == 0) {             return "buzz";         } else if (number % 3 == 0) {             return "fizz";         }         return String.valueOf(number);  }

You tin regard that it's much cleaner fifty-fifty though it is likewise doing the examination twice, but it's to a greater extent than pleasing to the optic together with straightforward.

 is i of the most oft asked questions on  How to solve FizzBuzz inward Java?


Java Program to solve FizzBuzzProblem

Here is our consummate Java computer program which combines both approaches to solving the fizzbuzz problem. We likewise select some unit of measurement tests to verify our solution meets the work statements.
import org.junit.Test; import static org.junit.Assert.*; /**  * Java Program to solve FizzBuzz work  *   * @author WINDOWS 8  *  */ public class FizzBuzzTest {      /**      * Java Method to solve FizzBuzz problem, which states that computer program      * should impress fizz if discover is multiple of 3,       * impress buzz if discover is multiple      * of 5, together with impress fizzbuzz if discover is multiple of both 3 together with 5      *       * @param discover      * @return      */     public static String fizzBuzz(int number) {          if (number % 3 == 0) {             if (number % 5 == 0) {                 return "fizzbuzz";             } else {                 return "fizz";             }         } else if (number % 5 == 0) {             return "buzz";         }         return String.valueOf(number);     }      /**      * An improved version of before solution, much cleaner than previous      * version because nosotros select tested for divisible past times 3 together with 5 first.      * It avoid duplication also.      */     public static String fizzBuzz2(int number) {         if (number % 15 == 0) {             return "fizzbuzz";         } else if (number % 5 == 0) {             return "buzz";         } else if (number % 3 == 0) {             return "fizz";         }         return String.valueOf(number);     }               @Test     public void testFizzBuzz(){         assertEquals("fizz", fizzBuzz(3));         assertEquals("buzz", fizzBuzz(5));         assertEquals("fizzbuzz", fizzBuzz(15));         assertEquals("2", fizzBuzz(2));     }          @Test     public void testFizzBuzzV2(){         assertEquals("fizz", fizzBuzzV2(3));         assertEquals("buzz", fizzBuzzV2(5));         assertEquals("fizzbuzz", fizzBuzzV2(15));         assertEquals("2", fizzBuzzV2(2));     } }  Test Result : All Pass 

That's all almost how to solve FizzBuzz Problem inward Java.  Sometime FizzBuzz is likewise asked equally next work statement, write a computer program that prints the numbers from 1 to 100. But for multiples of 3 impress “Fizz” instead of the discover together with for the multiples of v impress “Buzz”. For numbers which are multiples of both 3 together with v impress “FizzBuzz". So don't confuse there, same logic plant fine.


Related Coding Problems from Programming Interviews
If you lot are a calculator scientific discipline graduate together with serious almost doing good on coding based questions on programming project interviews, essay to solve next problems:
  • How to cheque if a given discover is prime number or not? (solution)
  • Write a computer program to impress the highest frequency discussion from a text file? (solution)
  • How to honour if given String is a palindrome inward Java? (solution)
  • How to calculate factorial using recursion together with iteration? (solution)
  • How to contrary an int variable inward Java? (solution)
  • How to honour the highest together with lowest discover from int array? (answer)
  • How create you lot swap 2 integers without using a temporary variable? (solution)
  • Top 10 Programming problems from Java Interviews? (article)
  • Write a computer program to cheque if a discover is the mightiness of 2 or not? (solution)
  • How to cheque if a twelvemonth is a confine twelvemonth inward Java? (answer)
  • How to contrary String inward Java without using StringBuffer? (solution)
  • Write code to implement Bubble variety algorithm inward Java? (code)
  • How create you lot contrary discussion of a judgement inward Java? (solution)
  • Write code to implement Quicksort algorithm inward Java? (algorithm)
  • How to honour a missing discover inward a sorted array? (solution)
  • Write a computer program to code insertion variety algorithm inward Java (program)


Books for preparing Coding interviews
If you lot are non going Google, Microsoft or Amazon which inquire real tough coding questions but preparing for all other software companies, next 2 books volition help you lot to a greater extent than inward brusque time:
  • Programming Interviews Exposed: Secrets to Landing Your Next Job (see here
  • Cracking the Coding Interview: 150 Programming Questions together with Solutions (see here)

Subscribe to receive free email updates:

0 Response to "How to solve FizzBuzz inwards Java?"

Posting Komentar