Tutorialspoint
Problem
Solution
Submissions

FizzBuzz Implementation

Certification: Basic Level Accuracy: 57.14% Submissions: 35 Points: 5

Write a Java program to implement the classic FizzBuzz problem. For numbers from 1 to n, print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "FizzBuzz" for multiples of both.

Example 1
  • Input: n = 15
  • Output:
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
    Fizz
    Buzz
    11
    Fizz
    13
    14
    FizzBuzz
  • Explanation:
    • Multiples of 3 → "Fizz"
    • Multiples of 5 → "Buzz"
    • Multiples of both → "FizzBuzz"
Example 2
  • Input: n = 8
  • Output:
    1
    2
    Fizz
    4
    Buzz
    Fizz
    7
    8
  • Explanation:
    • Apply same logic for smaller range
Constraints
  • 1 ≤ n ≤ 10^5
  • Time Complexity: O(n)
  • Space Complexity: O(1)
NumberHCL TechnologiesKPMG
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Loop through each number from 1 to n
  • Check if the number is divisible by both 3 and 5
  • If not, check if the number is divisible by 3
  • If not, check if the number is divisible by 5
  • If none of the above conditions are met, print the number itself

Steps to solve by this approach:

 Step 1: Initialize a loop to iterate from 1 to n.
 Step 2: For each number, check if it's divisible by both 3 and 5 using the modulo operator.
 Step 3: If the number is divisible by both 3 and 5, print "FizzBuzz".
 Step 4: If the number is divisible by only 3, print "Fizz".
 Step 5: If the number is divisible by only 5, print "Buzz".
 Step 6: If the number is not divisible by either 3 or 5, print the number itself.
 Step 7: Continue this process for all numbers from 1 to n.

Submitted Code :