Simple Discount Calculator Based on Purchase Amount

Simple Discount Calculator Based on Purchase Amount


Introduction

Discounts are an important concept in both programming and business. In retail or online shopping, discounts help customers save money and encourage more purchases. In this lesson, you’ll learn how to create a simple discount calculator in C# using if and if-else statements. This lesson will guide you through applying conditional logic to determine a discount based on a given purchase amount.

Understanding how to use conditional statements is a core programming skill. In this context, these conditions allow your program to “decide” what discount should be applied depending on how much a customer spends. This activity not only teaches logical thinking but also simulates real-life business logic in software development.


Objectives

Before we start coding, let’s identify what you should achieve by the end of this lesson. The objectives are based on the Outcome-Based Education (OBE) approach and focus on four key learning outcomes — understand, learn, practice, and apply.

  • Understand: Recognize how conditional statements control decision-making in a program.
  • Learn: Identify how to calculate and apply discounts based on different purchase amounts.
  • Practice: Write a C# console application that computes the total price after applying a discount.
  • Apply: Modify the discount logic to handle new conditions, such as loyalty points or promo codes.

These objectives aim to strengthen both your logical reasoning and practical coding abilities.


Beginner-Friendly Source Code and Step-by-Step Instructions

Step-by-Step Instructions

  1. Open Visual Studio Code.
    Ensure you have the .NET SDK installed. To verify, type in your terminal:

    dotnet --version
    
  2. Create a new project folder.
    Example: D:\2025-1_doe_bsis_1a
  3. Open the folder in VS Code.
    Go to File → Open Folder…
  4. Create a new console project.
    dotnet new console -n discount_calculator
    
  5. Navigate to your project folder.
    cd discount_calculator
    
  6. Open Program.cs and replace the code with the sample below.

C# Program: Simple Discount Calculator

using System;

namespace DiscountCalculatorApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Simple Discount Calculator ===");

            Console.Write("Enter total purchase amount: ");
            double purchaseAmount = Convert.ToDouble(Console.ReadLine());

            double discount = 0.0;

            if (purchaseAmount >= 1000)
            {
                discount = 0.20; // 20% discount
            }
            else if (purchaseAmount >= 500)
            {
                discount = 0.10; // 10% discount
            }
            else if (purchaseAmount >= 200)
            {
                discount = 0.05; // 5% discount
            }
            else
            {
                discount = 0.0; // No discount
            }

            double discountAmount = purchaseAmount * discount;
            double totalAmount = purchaseAmount - discountAmount;

            Console.WriteLine($"\nDiscount: {discount * 100}%");
            Console.WriteLine($"Discount Amount: ₱{discountAmount:F2}");
            Console.WriteLine($"Total Amount to Pay: ₱{totalAmount:F2}");
            Console.WriteLine("\nThank you for shopping!");
        }
    }
}

How to Run the Program

  • Open the terminal inside VS Code.
  • Type:
    dotnet run
    

Example Output

Example 1: Purchase = 1,200

=== Simple Discount Calculator ===
Enter total purchase amount: 1200
Discount: 20%
Discount Amount: ₱240.00
Total Amount to Pay: ₱960.00
Thank you for shopping!

Example 2: Purchase = 450

=== Simple Discount Calculator ===
Enter total purchase amount: 450
Discount: 5%
Discount Amount: ₱22.50
Total Amount to Pay: ₱427.50
Thank you for shopping!

Example 3: Purchase = 150

=== Simple Discount Calculator ===
Enter total purchase amount: 150
Discount: 0%
Discount Amount: ₱0.00
Total Amount to Pay: ₱150.00
Thank you for shopping!

Explanation of the Program

This program demonstrates how to use if-else conditional statements to make decisions based on input values. Here’s how it works:

  1. The program prompts the user to input the total purchase amount.
  2. The entered value is converted from text to a numeric value using Convert.ToDouble().
  3. Conditional statements check the amount:
    • If the amount is ₱1000 or more, a 20% discount is applied.
    • If the amount is ₱500 to ₱999, a 10% discount is applied.
    • If the amount is ₱200 to ₱499, a 5% discount is applied.
    • Otherwise, there is no discount.
  4. The program then calculates the discount amount and total price, displaying all results in a formatted manner.

This is a great beginner exercise for learning decision control, comparison operators, and basic arithmetic in C#.


Summary

In this lesson, you created a simple discount calculator in C# that uses conditional statements to determine how much discount to apply based on the total purchase amount. You learned how to combine arithmetic and logic to solve a real-world problem. This program demonstrates how developers use conditional structures to make smart business decisions inside applications. Mastering these concepts will prepare you for more complex programs involving nested conditions and user-defined functions.


Quiz (5 Items – Bloom’s Taxonomy)

  1. What data type is used to store purchase amounts and discounts?
    A. int
    B. double
    C. string
    D. bool
  2. Which C# statement allows different actions based on purchase amount?
    A. for
    B. while
    C. if-else
    D. switch
  3. What is the discount percentage if the user enters 700?
    A. 5%
    B. 10%
    C. 20%
    D. 0%
  4. Which line converts text input to a double value?
    A. Convert.ToInt32()
    B. Convert.ToDouble()
    C. ToString()
    D. Console.Read()
  5. What happens if the purchase amount is less than 200?
    A. 10% discount applied
    B. 5% discount applied
    C. No discount
    D. Invalid input

Exercises, Assessment, and Lab Exam

Introduction to Exercises

The following activities help reinforce your understanding of how conditional statements and arithmetic operations work together. These exercises are designed to encourage logic-building and creative problem-solving in business-oriented programming.

Exercises

  1. Modify the program to include a 30% discount for purchases above ₱2000.
  2. Add an option to calculate the total savings and display it separately.
  3. Display a message like “You’ve earned a loyalty discount!” when a discount is applied.
  4. Allow the user to input multiple items and compute the total purchase before applying a discount.
  5. Create a version that uses user-defined methods for computing the discount.

Assessment

  • Quiz: Answer the 5 multiple-choice questions above.
  • Performance Task: Create a C# console application that accepts a purchase amount and applies varying discounts (5%, 10%, 15%, and 20%) depending on the amount. Ensure proper formatting of results.

Lab Exam

Write a C# program that:

  • Accepts the total purchase amount.
  • Applies a 10% discount for purchases above ₱500 and a 20% discount for purchases above ₱1000.
  • Displays the original amount, discount amount, and total payable.
  • Uses clear if-else structures and formatted outputs.

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