Java FizzBuzz Challenge

JavaJavaBeginner
Practice Now

Introduction

Welcome to CodeCarnival, the most exciting coding fair in JavaLand! As a rising star in the programming world, you've been invited to showcase your skills at the famous FizzBuzz booth.

The FizzBuzz game is a classic coding challenge that tests a programmer's understanding of loops and conditionals. Your task is to complete a program that counts from 1 to 100, but with a twist! For multiples of three, it should print "Fizz" instead of the number, and for multiples of five, it should print "Buzz". For numbers which are multiples of both three and five, it should print "FizzBuzz".

Are you ready to dazzle the crowd with your coding prowess? Let's dive in and create some FizzBuzz magic!


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java/BasicSyntaxGroup -.-> java/for_loop("`For Loop`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") subgraph Lab Skills java/for_loop -.-> lab-413852{{"`Java FizzBuzz Challenge`"}} java/if_else -.-> lab-413852{{"`Java FizzBuzz Challenge`"}} java/operators -.-> lab-413852{{"`Java FizzBuzz Challenge`"}} java/output -.-> lab-413852{{"`Java FizzBuzz Challenge`"}} java/variables -.-> lab-413852{{"`Java FizzBuzz Challenge`"}} end

Complete the FizzBuzz Program

This classic programming problem is an excellent way to practice your conditional logic and loop control. In this exercise, you'll implement the famous FizzBuzz game, which is not only a fun coding task but also a common interview question. Let's dive in and bring the FizzBuzz logic to life!

Tasks

  • Open the pre-created file FizzBuzz.java in the ~/project directory.
  • Find the TODO comment in the code.
  • Add the missing code to implement the FizzBuzz logic inside the for loop.

Requirements

  • The file FizzBuzz.java should already exist in the ~/project directory.
  • You should only add code where the TODO comment is, inside the for loop.
  • Your code should correctly implement the FizzBuzz rules:
    • For multiples of 3, print "Fizz"
    • For multiples of 5, print "Buzz"
    • For multiples of both 3 and 5, print "FizzBuzz"
    • For all other numbers, print the number itself

Example

When completed correctly, your program should produce output like this (showing first 15 numbers):

cd ~/project
javac FizzBuzz.java
java FizzBuzz

Sample Output:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...
✨ Check Solution and Practice

Summary

In this challenge, you've implemented the classic FizzBuzz program, which is a great exercise in using loops and conditional statements in Java. This challenge reinforced key concepts from your Java Control Flow lab:

  1. Using a for loop to iterate through a range of numbers
  2. Using if-else statements to make decisions based on certain conditions
  3. Using the modulo operator (%) to check for divisibility

By completing this challenge, you've not only practiced these fundamental Java skills but also created a program that's often used in coding interviews. FizzBuzz is a simple but effective way to demonstrate your understanding of basic programming concepts.

Other Java Tutorials you may like