Day 8: FizzBuzz

Objective

Today’s challenge is to implement the classic FizzBuzz program, a popular coding problem that tests your understanding of conditional statements and loops. You’ll write a program that prints numbers from 1 to a specified limit, but with a twist:

  1. Print “Fizz” for numbers divisible by 3.
  2. Print “Buzz” for numbers divisible by 5.
  3. Print “FizzBuzz” for numbers divisible by both 3 and 5.
  4. Otherwise, just print the number.

Why FizzBuzz Is Important

FizzBuzz isn’t just a fun game—it’s a foundational problem that helps you:

  1. Understand Conditional Logic: Use if-else statements effectively.
  2. Combine Conditions: Learn to apply logical operators (AND, OR) for more complex conditions.
  3. Work with Loops: Iterate over a range of numbers to automate repetitive tasks.

By completing FizzBuzz, you’ll build confidence in creating programs that combine loops and conditionals.


Steps to Solve

1. Understand the Problem

  • Loop through numbers starting from 1 up to a limit (e.g., 1 to 20).
  • For each number:
    • Check if it’s divisible by both 3 and 5. If true, print “FizzBuzz”.
    • Check if it’s divisible by 3. If true, print “Fizz”.
    • Check if it’s divisible by 5. If true, print “Buzz”.
    • Otherwise, print the number itself.

2. Use Modulus Operator

The modulus operator (%) helps check divisibility:

  • number % 3 == 0 means the number is divisible by 3.
  • number % 5 == 0 means the number is divisible by 5.

3. Order Matters

To avoid missing “FizzBuzz”, check for divisibility by both 3 and 5 first, before checking for 3 or 5 individually.


Code Examples

Python Example:

# Get the limit from the user
limit = int(input("Enter the limit for FizzBuzz: "))

# Loop through numbers from 1 to limit
for number in range(1, limit + 1):
    if number % 3 == 0 and number % 5 == 0:
        print("FizzBuzz")
    elif number % 3 == 0:
        print("Fizz")
    elif number % 5 == 0:
        print("Buzz")
    else:
        print(number)

Java Example:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Get the limit from the user
        System.out.print("Enter the limit for FizzBuzz: ");
        int limit = scanner.nextInt();

        // Loop through numbers from 1 to limit
        for (int number = 1; number <= limit; number++) {
            if (number % 3 == 0 && number % 5 == 0) {
                System.out.println("FizzBuzz");
            } else if (number % 3 == 0) {
                System.out.println("Fizz");
            } else if (number % 5 == 0) {
                System.out.println("Buzz");
            } else {
                System.out.println(number);
            }
        }
    }
}

JavaScript Example:

// Get the limit from the user
let limit = parseInt(prompt("Enter the limit for FizzBuzz:"));

// Loop through numbers from 1 to limit
for (let number = 1; number <= limit; number++) {
    if (number % 3 === 0 && number % 5 === 0) {
        console.log("FizzBuzz");
    } else if (number % 3 === 0) {
        console.log("Fizz");
    } else if (number % 5 === 0) {
        console.log("Buzz");
    } else {
        console.log(number);
    }

Edge Cases to Consider

  • Non-Positive Limits: What if the user enters 0 or a negative number?
    • Add validation to ensure the user enters a positive number.
  • High Limits: Test your program with a large limit (e.g., 1000) to ensure it runs efficiently.

Extensions to Explore

  1. Custom Divisors: Allow the user to specify their own divisors instead of 3 and 5.
  2. Custom Words: Let the user input the words for each divisor.
    • For example: Divisible by 4 = “Quad”, Divisible by 6 = “Hex”.
  3. Store Results: Save the FizzBuzz sequence in a list or array instead of printing it, and display it at the end.

What You’ve Learned

  • How to use loops to automate repetitive tasks.
  • How to apply conditional statements for decision-making.
  • How to combine conditions using logical operators (AND, OR).

FizzBuzz may be simple, but it’s a powerful exercise for building problem-solving and coding skills!


Next Steps

Get ready for Day 9: Factorial Finder, where you’ll write a program that calculates the factorial of a number: