Beginner Coding – FizzBuzz Program (Java)

When learning to program, it is important that one practice what they’ve learned and what they’re currently learning, as many programming concepts lay a foundation for other concepts that you’ll learn later. A FizzBuzz program is one type of programming exercise that one can do in order to re-enforce concepts that were learned early on in their programming education.

A FizzBuzz program should:

  • Print the numbers 1 – 100
  • If a number is divisible by 3, the program should print the word “Fizz”
  • If a number is divisible by 5, the program should print the word “Buzz”
  • If a number is divisible by both 3 and 5, it should print the word “FizzBuzz”

Here’s an example of one possible solution to the FizzBuzz program, written in Java, as well as explanations as to what the code does and why it is used.


class FizzBuzz{
 
 public static void main (String[] args){
 
    //Use a "for loop" to print the numbers 1 - 100 to the screen/console,
    //as we know how many numbers will be printed at runtime.
    for(int i = 1; i <= 100; i++){

            //Check to see if a number is divisible by 3 and 5, which is done
            //first because checking for divisibility for either 3 or 5 first
            //would cause either "Fizz" or "Buzz" to be printed instead
            if(i % 3 == 0 && i % 5 == 0){
                System.out.println("FizzBuzz");
            }
            //Check for divisibility by 3 and print "Fizz" if true
            else if(i % 3 == 0){
               System.out.println("Fizz");
            }
            //Check for divisibility by 5 and print "Buzz" if true
            else if(i % 5 == 0){
                System.out.println("Buzz");
            }
            //If a number is not divisible by 3 or 5, print it normally
            else{
                System.out.println(i);
            }

     }
 
   }
}

The resulting output from this code would be:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz
Fizz
22
23
Fizz
Buzz
26
Fizz
28
29
FizzBuzz
31
32
Fizz
34
Buzz
Fizz
37
38
Fizz
Buzz
41
Fizz
43
44
FizzBuzz
46
47
Fizz
49
Buzz
Fizz
52
53
Fizz
Buzz
56
Fizz
58
59
FizzBuzz
61
62
Fizz
64
Buzz
Fizz
67
68
Fizz
Buzz
71
Fizz
73
74
FizzBuzz
76
77
Fizz
79
Buzz
Fizz
82
83
Fizz
Buzz
86
Fizz
88
89
FizzBuzz
91
92
Fizz
94
Buzz
Fizz
97
98
Fizz
Buzz

Another way to check for divisibility by both 3 and 5, which would print “FizzBuzz”, one could instead check to see if a number is divisible by 15, the least common multiple of 3 and 5, which would result in the same output. Here’s what this would look like in Java code:


if(i % 15 == 0){
    System.out.println("FizzBuzz");
}

There you have it! That’s how to go about creating a “FizzBuzz” program in Java! Feel free to copy and paste the code written above into your IDE or editor of choice and play around with it, in order to what changing various lines of code could do! You could even include other checks, perhaps printing something else if a number is divisible by other numbers!

Please feel free to let me know what you think of this post in the comments or by sending me an e-mail using the Contact page, or on Twitter – @Jamiemcg! If you liked this post, I invite you to check out the TechnicalCafe YouTube channel, where you can find more tutorials on Java, HTML, CSS, and more!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.