Menu
Coddy logo textTech

Recap - Repeating Code

Part of the Fundamentals section of Coddy's Dart journey — lesson 47 of 94.

challenge icon

Challenge

Medium

In this challenge, you'll create a "FizzBuzz" program - a classic coding exercise that combines loops and conditional statements.

Write a program that:

  1. Takes an integer n as input
  2. Uses a loop to iterate from 1 to n
  3. For each number:
    • If it's divisible by 3, print "Fizz"
    • If it's divisible by 5, print "Buzz"
    • If it's divisible by both 3 and 5, print "FizzBuzz"
    • Otherwise, just print the number
  4. After the loop finishes, print the count of how many "Fizz", "Buzz", and "FizzBuzz" were printed

Example output for n = 15:

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
Fizz count: 4
Buzz count: 3
FizzBuzz count: 1

Try it yourself

import 'dart:io';

void main() {
  // Read the input number
  String? input = stdin.readLineSync();
  int n = int.parse(input!);
  
  // Initialize counters
  int fizzCount = 0;
  int buzzCount = 0;
  int fizzBuzzCount = 0;
  
  // Implement the FizzBuzz loop from 1 to n
  for (int i = 1; i <= n; i++) {
    bool divisibleBy3 = i % 3 == 0;
    bool divisibleBy5 = i % 5 == 0;
    
    // TODO: Complete the loop
  }
  
  // TODO: Print the counts at the end
}

All lessons in Fundamentals