Challenge: FizzBuzz
Part of the Logic & Flow section of Coddy's Dart journey — lesson 65 of 65.
Challenge
EasyCreate a program that implements the classic FizzBuzz challenge with proper loop structure and conditional logic. This challenge tests your ability to combine loops, conditional statements, and the modulo operator to solve a well-known programming problem.
Your task is to:
- Use a
forloop to iterate through numbers from 1 to 50 (inclusive) - For each number, apply the following rules in order:
- If the number is divisible by both 3 and 5, print
FizzBuzz - If the number is divisible by 3 only, print
Fizz - If the number is divisible by 5 only, print
Buzz - If the number is not divisible by 3 or 5, print the number itself
- If the number is divisible by both 3 and 5, print
- Each output should be on a separate line
- Use the modulo operator (
%) to check divisibility - Structure your conditional statements using
if,else if, andelsein the correct order
The first few lines of your output should look like this:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzzRemember that the key to solving FizzBuzz correctly is checking for the "both conditions" case (divisible by both 3 and 5) first, before checking the individual conditions. A number is divisible by another number if the remainder when divided by that number equals zero.
Your program should demonstrate proper use of loops for iteration, the modulo operator for divisibility testing, and conditional statements structured in the correct logical order to handle all possible cases.
Try it yourself
void main() {
// TODO: Write your code below
// Use a for loop to iterate from 1 to 50
// Check divisibility using the modulo operator (%)
// Remember to check for both 3 AND 5 first, then individual conditions
// Print each result on a separate line
}All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List