FizzBuzz
Zen C offers multiple ways to solve FizzBuzz. Here are two approaches: one using standard if/else statements and another using the match control flow feature.
Using if/else
This approach demonstrates Zen C's inclusive range loops (..=), basic modulo arithmetic, and implicit string interpolation.
fn main() {
for i in 1..=100 {
if i % 15 == 0 {
println "FizzBuzz";
} else if i % 3 == 0 {
println "Fizz";
} else if i % 5 == 0 {
println "Buzz";
} else {
println "";
}
}
}
Using match
This implementation utilizes the match statement, which serves as a powerful alternative to switch. It supports multiple value matching on a single branch using commas, and includes a catch-all wildcard (_).
fn main() {
for i in 1..=100 {
match i % 15 {
0 => { println "FizzBuzz" },
3, 6, 9, 12 => { println "Fizz" },
5, 10 => { println "Buzz" },
_ => { println "" },
}
}
}
Attribution: This is a community solution for the Rosetta Code task FizzBuzz in Zen C.
This article uses material from the Rosetta Code article FizzBuzz, which is released under the GNU Free Documentation License 1.3. A list of the original authors can be found in the page history.