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:
FizzBuzz isn’t just a fun game—it’s a foundational problem that helps you:
AND
, OR
) for more complex conditions.By completing FizzBuzz, you’ll build confidence in creating programs that combine loops and conditionals.
1
up to a limit (e.g., 1 to 20
).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.To avoid missing “FizzBuzz”, check for divisibility by both 3 and 5 first, before checking for 3 or 5 individually.
# 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)
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);
}
}
}
}
// 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);
}
AND
, OR
).FizzBuzz may be simple, but it’s a powerful exercise for building problem-solving and coding skills!
Get ready for Day 9: Factorial Finder, where you’ll write a program that calculates the factorial of a number: