Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* fizzBuzz function prints the standard output, the number 1 to N (one per line) with the following
- * restrictions
- * * for multiples of three print "Fizz" instead of the number
- * * for multiples of five print "Buzz" instead of the number
- * * for numbers that are multiples of both three and five print "FizzBuzz"
- */
- import java.util.*;
- public class FizzBuzz {
- public static final java.util.Scanner input = new java.util.Scanner(System.in);
- public static void main(String[] args) {
- int n;
- System.out.print("Enter n: ");
- n = input.nextInt();
- fizzBuzz(n);
- }
- public static void fizzBuzz(int n) {
- // Run through i to n and print accordingly
- for(int i = 1; i <= n; i++) {
- if(i % 3 == 0 && i % 5 == 0) {
- System.out.println("FizzBuzz");
- }
- else if(i % 3 == 0) {
- System.out.println("Fizz");
- }else if(i % 5 == 0) {
- System.out.println("Buzz");
- }else{
- System.out.println(i);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment