top of page
Click here to go to the home page of AskTheCode.

The FizzBuzz Game in Java | Ask The Code

Updated: Mar 9, 2021

Basic Java Programming | Problem Solving in Java | Problem Solving in Java | AskTheCode

Problem :

Remember the game of FizzBuzz from the last time? Well, I thought of some changes in the game, and also with the help of loops as well. Firstly, you'll be asking for a random integer and then loop from 1 until that integer. Then, implement these conditions in the game:


- print "Fizz" if the number is divisible by 3

- print "Buzz" if the number is divisible by 5

- print "FizzBuzz" if the number is divisible by both 3 and 5

- print the number itself if none of the above conditions are met


Input Format: A line containing an integer.


Input Sample:

15


Output Format: Multiple lines containing a string or an integer.


Output Sample:

1

2

Fizz

4

Buzz

Fizz

7

8

Fizz

Buzz

11

Fizz

13

14

FizzBuzz

Code:

import java.util.Scanner;
 
public class solution_FizzBuzz{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);

		int n = sc.nextInt();
		for (int count = 1; count <= n; count++) {
			if (count % 3 == 0 && count % 5 == 0) {
				System.out.println("FizzBuzz");
			}
			else if (count % 3 == 0) {
				System.out.println("Fizz");
			}
			else if (count % 5 == 0) {
				System.out.println("Buzz");
			}
			else{
				System.out.println(count);
			}
		}
	}
}

Recent Posts

See All

Comentarios


bottom of page