Weekly Expense Tracker

For Loop in C#: Weekly Expense Tracker

Introduction

In many real-life situations, people need to monitor and analyze their expenses over a specific period. One practical example is tracking daily expenses throughout a week. Instead of manually calculating the total, a simple program can automate the process and provide accurate results instantly. In programming, repetitive tasks like collecting daily expenses can be efficiently handled using loops.

In this lesson, we will learn how to use a for loop in C# to create a Weekly Expense Tracker. The program will allow the user to input expenses for seven days and automatically calculate the total weekly expense. Because the number of days in a week is fixed, the for loop is the most appropriate structure for iterating through the inputs.

The for loop is commonly used when the number of repetitions is known in advance. It consists of three main components: initialization, condition, and iteration. This makes it ideal for problems such as counting days, processing items in a list, or performing calculations repeatedly within a fixed range. Through this lesson, students will understand how loops simplify repetitive tasks while ensuring organized and efficient code.

By the end of this activity, students will be able to write a console program that accepts daily expenses, computes totals, and demonstrates how loops can be applied in financial tracking systems.


Lesson Objectives

Outcome-Based Education (OBE) focuses on what learners should be able to demonstrate after completing a lesson. The objectives in this section guide students from conceptual understanding to practical application. They ensure that learners not only recognize how a for loop works but can also apply it to real-world problems such as computing weekly expenses.

Through the Weekly Expense Tracker activity, students will develop foundational programming skills including input processing, accumulation of values, and structured iteration using loops.

1. Understand

  • Explain the syntax and structure of a for loop in C#.
  • Describe how loops help automate repetitive tasks.
  • Identify situations where a for loop is appropriate to use.

2. Learn

  • Learn how to declare variables for storing expenses and totals.
  • Understand how accumulation works when calculating totals inside a loop.
  • Recognize how loop counters control program repetition.

3. Practice

  • Write a simple console program that accepts daily expenses.
  • Use a for loop to repeat input for seven days.
  • Test the program with different expense values.

4. Apply

  • Apply the weekly expense tracking logic to other financial computations such as monthly budgets or savings tracking.
  • Modify the program to compute averages or identify the highest expense.
  • Integrate loop logic into larger programs.

Beginner-Friendly Source Code

Below is a simple C# console program that implements a Weekly Expense Tracker using a for loop.

using System;

namespace WeeklyExpenseTracker
{
    class Program
    {
        static void Main(string[] args)
        {
            double dailyExpense;
            double totalExpense = 0;

            Console.WriteLine("=== Weekly Expense Tracker ===\n");

            for (int day = 1; day <= 7; day++)
            {
                Console.Write($"Enter expense for Day {day}: ");
                dailyExpense = Convert.ToDouble(Console.ReadLine());

                totalExpense += dailyExpense;
            }

            Console.WriteLine("\nTotal Weekly Expense: " + totalExpense);
        }
    }
}

Code Explanation

Namespace
The namespace WeeklyExpenseTracker organizes the program into a logical grouping.

Variables

  • dailyExpense stores the amount entered by the user.
  • totalExpense accumulates all expenses entered during the loop.

For Loop

for (int day = 1; day <= 7; day++)

This loop runs seven times, representing the seven days of the week.

Accumulation

totalExpense += dailyExpense;

Each daily expense is added to the total.

Output
After the loop completes, the program prints the total weekly expense.


Instructions to Run the Program

  1. Open Visual Studio or any C# development environment.
  2. Create a Console Application project.
  3. Replace the default code with the provided source code.
  4. Press Ctrl + F5 to run the program.
  5. Enter the expense amount for each day when prompted.
  6. Observe the total weekly expense displayed after the inputs.

Example Output

=== Weekly Expense Tracker ===

Enter expense for Day 1: 100
Enter expense for Day 2: 120
Enter expense for Day 3: 80
Enter expense for Day 4: 150
Enter expense for Day 5: 90
Enter expense for Day 6: 110
Enter expense for Day 7: 130

Total Weekly Expense: 780

Summary

In this lesson, we explored how to use a for loop in C# to create a simple Weekly Expense Tracker. By collecting daily expenses for seven days and accumulating the values inside the loop, the program automatically computes the total weekly expense. This example demonstrates how loops simplify repetitive tasks and ensure efficient data processing. The concept learned here can easily be extended to more advanced applications such as budgeting systems, financial monitoring tools, and expense management software.


Multiple Choice Questions

1. Remembering

Which loop is most suitable when the number of repetitions is known?
A. while loop
B. do while loop
C. for loop
D. switch statement


2. Understanding

Why is a for loop used in the Weekly Expense Tracker program?
A. Because expenses are unknown
B. Because the program needs to run forever
C. Because the number of days is fixed
D. Because loops cannot use conditions


3. Applying

Which statement adds the daily expense to the total?
A. dailyExpense = totalExpense
B. totalExpense += dailyExpense
C. dailyExpense++
D. totalExpense--


4. Analyzing

What will happen if the loop condition is changed to day <= 5?
A. The program will crash
B. The program will accept expenses for five days only
C. The loop will run infinitely
D. No expenses will be recorded


5. Evaluating

Which modification would improve the program’s functionality?
A. Remove the loop
B. Add input validation for expenses
C. Print only the last expense
D. Remove variables


Exercises, Assessment, and Lab Exam

Introductory Paragraph

To deepen understanding of the for loop, students should perform additional exercises that expand the functionality of the Weekly Expense Tracker. These activities encourage experimentation with logic, calculations, and program enhancements. By modifying and extending the program, learners develop stronger programming skills and problem-solving abilities.


Exercises

  1. Modify the program to compute the average daily expense.
  2. Add functionality to determine the highest daily expense.
  3. Display the lowest expense recorded during the week.
  4. Format the total expense using currency formatting.
  5. Store expenses in an array and display them after computation.

Assessment Activity

Create a Monthly Expense Tracker that:

  • Accepts expenses for 30 days
  • Calculates total monthly expenses
  • Computes the average daily expense
  • Displays the highest expense recorded

Lab Exam

Task: Develop an Enhanced Expense Monitoring System

Requirements:

  • Accept daily expenses for 7 days
  • Display total weekly expense
  • Display average expense
  • Identify the highest and lowest expenses
  • Use proper loop structure and readable code

Evaluation Criteria:

  • Loop implementation (30%)
  • Correct calculations (30%)
  • Code readability and structure (20%)
  • Program functionality (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.

, , , , , , , , ,

Post navigation