Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Fizz Buzz Program in Java
Next article icon

Fizz Buzz Program in Java

Last Updated : 26 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

FizzBuzz is a game popular amongst kids that also teaches them the concept of division. In recent times it has become a popular programming question. Following is the problem statement for the FizzBuzz problem.

Examples:

Input: 9
Output: Fizz
Explanation: The number is divisible by 3 only.

Input: 25
Output: Buzz
Explanation: The number is divisible by 5 only.

Input: 15
Output: FizzBuzz
Explanation: The number is divisible by both 3 and 5.

Input: 4
Output: 4
Explanation: The number is not divisible by both 3 and 5.

We will use the following method to solve this problem statement.

Method for FizzBuzz Problem

The method used here will be a simple greedy approach. We will check if the given conditions given above and then print the output accordingly. We will use the following steps to implement this algorithm.

  1. Declare the number.
  2. Check if the number is divisible by both 3 and 5. If yes then print FizzBuzz.
  3. Else check if the number is divisible by 3. If yes then print Fizz.
  4. Else check if number is divisible by 5. If yes then print Buzz.
  5. Else print the given number as it is.

The only condition here is we first have to check whether the number is divisible by both 5 and 3 or not as if we check for single number first it will become true and we will get wrong output.

Below is the implementation of FizzBuzz Program in Java:

Java
// Java Program to implement // FizzBuzz Problem  public class FizzBuzz {     public static void main(String[] args)     {         fizzBuzz(9);         fizzBuzz(25);         fizzBuzz(15);         fizzBuzz(4);     }      public static void fizzBuzz(int n)     {          // check if divisible by both 3 and 5.         if (n % 3 == 0 && n % 5 == 0) {             System.out.println("FizzBuzz");         }          // check if divisible by both 3.         else if (n % 3 == 0) {             System.out.println("Fizz");         }          // check if divisible by both 5.         else if (n % 5 == 0) {             System.out.println("Buzz");         }          // If not divisible by anything print the number as         // it is.         else {             System.out.println(n);         }     } } 

Output:

Fizz
Buzz
FizzBuzz
4

Complexity of the above Method:

Time complexity: O(1)
Space complexity: O(1)


Next Article
Fizz Buzz Program in Java

B

bhushanc2003
Improve
Article Tags :
  • Java
  • Java Programs
  • Java Examples
Practice Tags :
  • Java

Similar Reads

    Java File Handling Programs
    Java is a programming language that can create applications that work with files. Files are containers that store data in different formats, such as text, images, videos, etc. Files can be created, read, updated, and deleted using Java. Java provides the File class from the java.io package to handle
    3 min read
    Java Program to Implement Circular Buffer
    When data is constantly moved from one place to another or from one process to another or is frequently accessed, it cannot be stored in permanent memory locations such as hard drives as they take time to retrieve the data. This type of data needs to be accessed quickly and is stored in temporary me
    8 min read
    Java Threading Programs - Basic to Advanced
    Java threading is the concept of using multiple threads to execute different tasks in a Java program. A thread is a lightweight sub-process that runs within a process and shares the same memory space and resources. Threads can improve the performance and responsiveness of a program by allowing paral
    3 min read
    Java Networking Programs - Basic to Advanced
    Java allows developers to create applications that can communicate over networks, connecting devices and systems together. Whether you're learning about basic connections or diving into more advanced topics like client-server applications, Java provides the tools and libraries you need. This Java Ne
    3 min read
    Number Guessing Game in Java
    A number-guessing game in Java is a simple program where the computer randomly selects a number, and the user has to guess it within a limited number of attempts. The program provides feedback on whether the guessed number is too high or too low, guiding the user toward the correct answer.This proje
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences