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: 9Output: FizzExplanation: The number is divisible by 3 only. Input: 25Output: BuzzExplanation: The number is divisible by 5 only. Input: 15Output: FizzBuzzExplanation: The number is divisible by both 3 and 5. Input: 4Output: 4Explanation: The number is not divisible by both 3 and 5. We will use the following method to solve this problem statement. Method for FizzBuzz ProblemThe 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. Declare the number.Check if the number is divisible by both 3 and 5. If yes then print FizzBuzz.Else check if the number is divisible by 3. If yes then print Fizz.Else check if number is divisible by 5. If yes then print Buzz.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: FizzBuzzFizzBuzz4Complexity of the above Method: Time complexity: O(1)Space complexity: O(1) Comment More infoAdvertise with us Next Article Fizz Buzz Program in Java B bhushanc2003 Follow 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 Like