Skip to content

Yet another programming solutions log

Sample bits from programming for the future generations.

Technologies Technologies
  • Algorithms and Data Structures
  • Java Tutorials
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Spock Framework Tutorial
  • Spring Framework
  • Bash Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Developer’s Tools
  • Productivity
  • About
Expand Search Form

Fizz Buzz in Java

farenda 2015-05-26 0

Problem:

How to write Fizz Buzz in Java?

The problem is as follows:

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’.

It’s a very common task on job interviews.

Solution:

It can be implemented in a few different ways. Here are sample two solutions:

  1. The simplest solution: fizzBuzz1()
  2. Interesting solution without if-else constructs!
package com.farenda.solved;

public class JavaSolved {

    private static String[] OUTPUTS = {
        "FizzBuzz", "%d", "%d", "Fizz", "%d", "Buzz", "%d",
        "%d", "%d", "Fizz", "Buzz", "%d", "Fizz", "%d", "%d"
    };

    public static void main(String[] args) {
        fizzBuzz1();
        fizzBuzz2();
    }

    private static void fizzBuzz1() {
        for (int i = 1; i <= 100; ++i) {
            if (i % 15 == 0) {
                System.out.println("FizzBuzz");
            } else if (i % 3 == 0) {
                System.out.println("Fizz");
            } else if (i % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(i);
            }
        }
    }

    private static void fizzBuzz2() {
        for (int i = 1; i <= 100; ++i) {
            System.out.printf(OUTPUTS[i % 15] + "%n", i);
        }
    }
}

Result:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
... // rest of the output you can imagine
Share with the World!
Categories Java Tags algorithms, java-basics
Previous: Bash define function
Next: HSQLDB Database Manager

Recent Posts

  • Java 8 Date Time concepts
  • Maven dependency to local JAR
  • Caesar cipher in Java
  • Java casting trick
  • Java 8 flatMap practical example
  • Linked List – remove element
  • Linked List – insert element at position
  • Linked List add element at the end
  • Create Java Streams
  • Floyd Cycle detection in Java

Pages

  • About Farenda
  • Algorithms and Data Structures
  • Bash Tutorial
  • Bean Validation Tutorial
  • Clojure Tutorial
  • Design Patterns
  • Java 8 Streams and Lambda Expressions Tutorial
  • Java Basics Tutorial
  • Java Collections Tutorial
  • Java Concurrency Tutorial
  • Java IO Tutorial
  • Java Tutorials
  • Java Util Tutorial
  • Java XML Tutorial
  • JUnit Tutorial
  • MongoDB Tutorial
  • Quartz Scheduler Tutorial
  • Software Developer’s Tools
  • Spock Framework Tutorial
  • Spring Framework

Tags

algorithms bash bean-validation books clojure design-patterns embedmongo exercises git gof gradle groovy hateoas hsqldb i18n java java-basics java-collections java-concurrency java-io java-lang java-time java-util java-xml java8 java8-files junit linux lists log4j logging maven mongodb performance quartz refactoring regex rest slf4j solid spring spring-boot spring-core sql unit-tests

Yet another programming solutions log © 2022

sponsored