thorax232

Java - FizzBuzz

Nov 3rd, 2013
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.96 KB | None | 0 0
  1. /* fizzBuzz function prints the standard output, the number 1 to N (one per line) with the following
  2.  * restrictions
  3.  * * for multiples of three print "Fizz" instead of the number
  4.  * * for multiples of five print "Buzz" instead of the number
  5.  * * for numbers that are multiples of both three and five print "FizzBuzz"
  6.  */
  7.  
  8.  import java.util.*;
  9.  
  10.  public class FizzBuzz {
  11.    
  12.     public static final java.util.Scanner input = new java.util.Scanner(System.in);
  13.  
  14.     public static void main(String[] args) {
  15.        
  16.         int n;
  17.    
  18.         System.out.print("Enter n: ");
  19.         n = input.nextInt();
  20.         fizzBuzz(n);
  21.     }
  22.    
  23.     public static void fizzBuzz(int n) {
  24.    
  25.         // Run through i to n and print accordingly
  26.         for(int i = 1; i <= n; i++) {
  27.             if(i % 3 == 0 && i % 5 == 0) {
  28.                 System.out.println("FizzBuzz");
  29.             }
  30.             else if(i % 3 == 0) {
  31.                 System.out.println("Fizz");
  32.             }else if(i % 5 == 0) {
  33.                 System.out.println("Buzz");
  34.             }else{
  35.                 System.out.println(i);
  36.             }
  37.         }
  38.     }
  39.  }
Advertisement
Add Comment
Please, Sign In to add comment