Java program to print n elements of Fizz Buzz sequence.

import java.util.Scanner;

// © 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net

public class FizzBuzz {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    int n;
    
    System.out.print("Enter number of numbers: ");
        n = input.nextInt();

        for (int i = 1; i <= n; i++)
        {
            if (i % 3 == 0 && i % 5 == 0)
            {
            	System.out.print("Fizz Buzz");
            }
            else if (i % 3 == 0)
            {
            	System.out.print("Fizz");
            }
            else if (i % 5 == 0)
            {
            	System.out.print("Buzz");
            }
            else
            {
            	System.out.print(i);
            }

            System.out.print(" ");
        }
  }
}

 

Java Fizz Buzz
Tagged on:     

Leave a Reply

Your email address will not be published. Required fields are marked *