Staircase step counter
A fitness app tracks someone climbing the stairs of an office block. Every flight in the building has the same number of steps. After each flight the app shows the running total of steps climbed so far, and when the climb is over it shows the final total.
Read the number of flights and the steps per flight, then print the running total after every flight followed by the final total.
Input
Two lines: an integer f (the number of flights) and an integer s (steps per flight).
Output
One line Flight i: total for each flight i from 1 to f, where total is the number of steps climbed after that flight, then a final line Total: total.
Example 1
Input
3 12
Output
Flight 1: 12 Flight 2: 24 Flight 3: 36 Total: 36
Each flight adds 12 steps; after three flights the total is 36.
Example 2
Input
0 15
Output
Total: 0
No flights were climbed, so only the final total is printed.
Constraints
- 0 <= f <= 100
- 0 <= s <= 1000
Hints
Hint 1 of 3
A for loop that counts from 1 to flights visits each flight once; use the loop variable in the message.
Hint 2 of 3
Keep a total variable declared before the loop and add stepsPerFlight to it on every pass.
Hint 3 of 3
The final line is printed after the loop, so it is correct even when there are zero flights.
Solution
Show a reference solution and explanation
int flights = int.Parse(Console.ReadLine()!);
int stepsPerFlight = int.Parse(Console.ReadLine()!);
int total = 0;
for (int i = 1; i <= flights; i++)
{
total += stepsPerFlight;
Console.WriteLine($"Flight {i}: {total}");
}
Console.WriteLine($"Total: {total}");
Why it works
The loop variable i starts at 1 rather than the usual 0 because it is shown to the user as a flight number. A variable declared before the loop survives every iteration, so total accumulates one flight's steps per pass and still holds the final value afterwards for the last line. When flights is 0 the loop condition i <= flights is false immediately, the body never runs, and only Total: 0 appears, which is why the last line must sit outside the loop.
Lesson for this exercise: Conditions and Loops in C#