Java FizzBuzz Challenge

JavaBeginner
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!

This is a Challenge, which differs from a Guided Lab in that you need to try to complete the challenge task independently, rather than following the steps of a lab to learn. Challenges are usually a bit difficult. If you find it difficult, you can discuss with Labby or check the solution. Historical data shows that this is a beginner level challenge with a 95% pass rate. It has received a 97% positive review rate from learners.

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.