...

/

Solution: Loops, Operators, and Exception Handling

Solution: Loops, Operators, and Exception Handling

Look at the solution to the previous challenge.

Loops solution

Let's have a look at the problem statement of the challenge.

Problem statement

FizzBuzz is a group game for children to teach them about division. Players take turns to count incrementally, replacing any number divisible by three with the word fizz, any number divisible by five with the word buzz, and any number divisible by both with fizzbuzz. Write code that outputs a simulated FizzBuzz game that counts up to 100100.

Press + to interact
FizzBuzz game
FizzBuzz game

Solution

The solution to the loop challenge is provided below:

<Project Sdk="Microsoft.NET.Sdk">

	<PropertyGroup>
		<OutputType>Exe</OutputType>
		<TargetFramework>net7.0</TargetFramework>
		<ImplicitUsings>enable</ImplicitUsings>
		<Nullable>enable</Nullable>
	</PropertyGroup>

	<ItemGroup>
		<Using Include="System.Console" Static="true" />
	</ItemGroup>

</Project>
FizzBuzz code

Explanation

  • Line 1: This line starts a for loop that iterates from i = 1 to i = 100, inclusive. int i = 1 initializes the loop variable i to 11. The loop will continue as long as i is less than or equal to 100100. i++ increments the value of i by 11 in each iteration.

  • Lines 2–18: This block of code is the core logic of the "FizzBuzz" program. It uses conditional statements (if, else if, else) to determine what to print for each value of i.

    • If i is divisible by both 33 ...