0
0
C++programming~20 mins

For loop in C++ - Mini Project: Build & Apply

Choose your learning style9 modes available
For Loop Practice in C++
📖 Scenario: You are helping a small store owner who wants to calculate the total price of items sold. Each item has a price, and the owner wants to add them up using a simple loop.
🎯 Goal: Build a C++ program that uses a for loop to sum the prices of items in an array and print the total.
📋 What You'll Learn
Create an array called prices with the exact values: 10, 20, 30, 40, 50
Create an integer variable called total and set it to 0
Use a for loop with the variable i to iterate over the prices array
Add each price to total inside the loop
Print the value of total after the loop
💡 Why This Matters
🌍 Real World
Summing prices is a common task in stores, shopping carts, and billing systems.
💼 Career
Understanding loops and arrays is essential for many programming jobs, especially those involving data processing.
Progress0 / 4 steps
1
Create the prices array
Create an integer array called prices with these exact values: 10, 20, 30, 40, 50.
C++
Need a hint?

Use the syntax int prices[] = {values}; to create the array.

2
Create the total variable
Create an integer variable called total and set it to 0.
C++
Need a hint?

Use int total = 0; to create the variable.

3
Use a for loop to sum prices
Use a for loop with the variable i to iterate over the prices array and add each price to total inside the loop.
C++
Need a hint?

Use for (int i = 0; i < 5; i++) and inside the loop add prices[i] to total.

4
Print the total
Write a std::cout statement to print the value of total.
C++
Need a hint?

Use std::cout << total << std::endl; to print the total sum.