For Loop in C#: Calculate Total Sales for 7 Days
Table of Contents
- For Loop in C#: Calculate Total Sales for 7 Days
- Introduction
- Lesson Objectives
- Introductory Paragraph on Objectives
- 1. Understand
- 2. Learn
- 3. Practice
- 4. Apply
- Beginner-Friendly Source Code
- Code Explanation
- Instructions to Run
- Example Output
- Summary
- Multiple Choice Questions (Bloom’s Taxonomy)
- Exercises, Assessment, and Lab Exam
- Introductory Paragraph
- Exercises
- Assessment Activity
- Lab Exam
Introduction
In business and retail operations, tracking daily sales and calculating weekly totals are fundamental tasks. Automating this process with programming reduces manual errors and improves efficiency. In this lesson, we will use a for loop in C# to calculate total sales for a week. The for loop allows repetition of a block of code a specific number of times, making it ideal for scenarios like computing totals over a fixed number of days.
This lesson demonstrates how programming can simplify day-to-day business calculations, providing a practical application of loops for beginners.
Lesson Objectives
The objectives are framed according to Outcome-Based Education (OBE) principles to ensure measurable learning outcomes. Students will progress from understanding concepts to applying them in real-world scenarios.
Introductory Paragraph on Objectives
The objectives aim to guide learners from conceptual comprehension to practical implementation. Students will understand the structure of a for loop, learn how to apply it to calculate totals, practice by implementing code, and apply the knowledge to similar financial computations.
1. Understand
- Explain the syntax and components of a
for loopin C#. - Differentiate between
for,while, anddo whileloops. - Describe the purpose of using loops for repetitive calculations.
2. Learn
- Identify variables needed to store daily sales and weekly totals.
- Learn how to accumulate values within a loop using summation.
- Recognize the importance of input validation in numeric computations.
3. Practice
- Write a console application that accepts daily sales input.
- Compute and display the total weekly sales.
- Test with different sets of input values to ensure correct totals.
4. Apply
- Apply the
for looplogic to other cumulative calculations such as monthly expenses. - Extend the program to calculate averages or identify the highest daily sales.
- Use this approach to handle multiple weeks or multiple products.
Beginner-Friendly Source Code
using System;
namespace WeeklySalesCalculator
{
class Program
{
static void Main(string[] args)
{
double dailySale;
double totalSales = 0;
Console.WriteLine("=== Weekly Sales Calculator ===\n");
for (int day = 1; day <= 7; day++)
{
Console.Write($"Enter sales for Day {day}: ");
while (!double.TryParse(Console.ReadLine(), out dailySale) || dailySale < 0)
{
Console.Write("Invalid input. Enter a positive number: ");
}
totalSales += dailySale;
}
Console.WriteLine($"\nTotal sales for the week: {totalSales:C2}");
}
}
}
Code Explanation
- Namespace:
WeeklySalesCalculatororganizes the program logically. - Variables:
dailySalestores input for each day.totalSalesaccumulates weekly totals.
- For Loop:
- Iterates from day 1 to 7.
- Prompts user for input and validates it using
double.TryParse().
- Accumulation:
totalSales += dailySalesums up daily inputs. - Output: Displays formatted total weekly sales using
{totalSales:C2}for currency format.
Instructions to Run
- Open Visual Studio or any C# IDE.
- Create a new Console Application project.
- Replace default code with the program above.
- Press Ctrl + F5 to run the program.
- Enter daily sales for 7 days. Observe the total weekly sales output.
Example Output
=== Weekly Sales Calculator ===
Enter sales for Day 1: 1000
Enter sales for Day 2: 1250.50
Enter sales for Day 3: 900
Enter sales for Day 4: 1100
Enter sales for Day 5: 950
Enter sales for Day 6: 1025.75
Enter sales for Day 7: 1200
Total sales for the week: $7,426.25
Summary
In this lesson, we explored how to use a for loop in C# to calculate total weekly sales. The loop efficiently iterates through seven days, prompting for daily sales input and accumulating the total. This approach ensures accurate, automated calculation and provides a foundation for more complex financial computations such as monthly sales summaries or product-level totals. By practicing this lesson, students gain practical experience in loop control structures, input validation, and basic arithmetic operations in C#.
Multiple Choice Questions (Bloom’s Taxonomy)
- (Remembering) Which loop is best when the number of iterations is known?
A. while loop
B. do while loop
C. for loop
D. foreach loop
Answer: C - (Understanding) Why is the
for loopsuitable for weekly sales calculation?
A. It checks condition after execution
B. It repeats a known number of times
C. It skips input validation
D. It cannot handle numeric operations
Answer: B - (Applying) Which operator is used to accumulate daily sales?
A.=
B.+=
C.-=
D.*=
Answer: B - (Analyzing) What happens if the loop counter is set incorrectly?
A. Program compiles but outputs wrong totals
B. Program always stops immediately
C. Input is automatically corrected
D. Compiler throws error
Answer: A - (Evaluating) How can the program be improved for multiple weeks?
A. Use nestedfor loops
B. Remove validation
C. Usewhileinstead
D. Store only last week sales
Answer: A
Exercises, Assessment, and Lab Exam
Introductory Paragraph
To reinforce the practical skills learned in this lesson, students should engage in exercises that expand the logic, add complexity, and improve code usability. These activities enhance problem-solving skills and deepen understanding of loop structures and cumulative operations.
Exercises
- Modify the program to calculate average daily sales.
- Include a feature to determine the day with highest sales.
- Extend the program for 30-day monthly sales.
- Add error handling for negative numbers and invalid inputs.
- Implement the program using arrays to store daily sales.
Assessment Activity
- Create a console application that:
- Accepts sales for 7 days for multiple products.
- Calculates weekly total per product.
- Calculates overall weekly sales.
- Uses
for loopseffectively with input validation.
Lab Exam
Task: Develop a Weekly Sales Module with the following:
Requirements:
- Input daily sales for 7 days for multiple categories (e.g., electronics, groceries).
- Calculate total weekly sales per category.
- Identify the highest-selling day per category.
- Format outputs clearly using currency symbols.
- Organize code with methods and proper namespace.
Evaluation Criteria:
- Loop implementation (30%)
- Accuracy of computation (30%)
- Code readability and organization (20%)
- Functionality and user interaction (20%)
You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.
Hire our team to do the project.