Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: for Loop | Loops
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C# Basics
Section 4. Chapter 2
single

single

bookChallenge: for Loop

Swipe to show menu

Task

Swipe to start coding

You are shopping and want to calculate the total cost of buying multiple items at the same price. To do this, implement the method CalculateTotalCost(int itemPrice, int quantity) that uses a for loop to add itemPrice to a running total, once for each item in quantity.

  • Initialize totalCost to 0.
  • The loop starts at i = 0 and runs while i < quantity — this ensures itemPrice is added exactly quantity times.
  • Add itemPrice to totalCost on each iteration.
  • Return totalCost at the end of the method.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt