Compute Total Price Using Quantity and Unit Price

C# Methods with Parameters: Compute Total Price Using Quantity and Unit Price


Introduction of the Lesson

In C# programming, methods are fundamental tools used to organize and reuse code efficiently. One of the most important concepts when working with methods is the use of parameters, which allow data to be passed into a method for processing. This makes programs more dynamic, flexible, and reusable.

In this lesson, you will learn how to create a method with parameters to compute the total price of a product based on quantity and unit price. The program will accept user input, perform calculations, and display the result. This practical example reflects real-world applications such as billing systems, point-of-sale (POS) systems, and inventory management software.


Lesson Objectives

This lesson is designed using the Outcome-Based Education (OBE) framework to ensure a structured and measurable learning experience. The objectives guide you from foundational understanding to practical application of methods with parameters in C#.

By the end of this lesson, you will not only understand how parameters work but also confidently implement them in a complete program that solves a real-world problem.

Objectives:

  1. Understand
    Define what method parameters are and explain their role in passing data into methods.
  2. Learn
    Identify how parameters are declared and used within a method in C#.
  3. Practice
    Develop a method that calculates total price using quantity and unit price.
  4. Apply
    Build a user-interactive program that accepts input and computes results accurately.

Understanding Methods with Parameters

A method with parameters allows you to pass values into a method so it can perform operations using those values. Parameters act as placeholders for the actual data (arguments) provided during method calls.

Basic Syntax:

returnType MethodName(dataType parameter1, dataType parameter2)
{
    // method body
}

In this lesson, we will use:

  • quantity (int)
  • unitPrice (double)

The method will compute:

Total Price = quantity × unitPrice

Beginner-Friendly C# Source Code

using System;

namespace TotalPriceCalculatorApp
{
    class Program
    {
        // Method with parameters
        static double ComputeTotalPrice(int quantity, double unitPrice)
        {
            double total = quantity * unitPrice;
            return total;
        }

        static void Main(string[] args)
        {
            Console.WriteLine("TOTAL PRICE CALCULATOR");
            
            // User input
            Console.Write("Enter quantity: ");
            int quantity = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter unit price: ");
            double unitPrice = Convert.ToDouble(Console.ReadLine());

            // Method call
            double totalPrice = ComputeTotalPrice(quantity, unitPrice);

            // Display result
            Console.WriteLine("Total Price: " + totalPrice);

            Console.ReadLine();
        }
    }
}

Step-by-Step Instructions

  1. Open Visual Studio or any C# IDE
  2. Create a new Console Application
  3. Copy and paste the code into your program
  4. Run the application
  5. Enter the quantity and unit price when prompted
  6. Observe the computed total price

Example Output

TOTAL PRICE CALCULATOR
Enter quantity: 3
Enter unit price: 150.50
Total Price: 451.5

Lesson Summary

Methods with parameters in C# allow developers to pass data into functions, making programs more flexible and reusable. In this lesson, you created a method that computes total price using quantity and unit price, demonstrating how parameters work in real-world scenarios. By integrating user input and method calls, you built a simple yet practical application that reflects common business logic. Mastering this concept is essential for developing efficient and scalable applications.


Multiple Choice Questions

  1. What is a parameter in C#?
    A. A loop
    B. A variable used to store user input only
    C. A value passed into a method
    D. A class name
  2. What does the method ComputeTotalPrice(int quantity, double unitPrice) do?
    A. Displays output only
    B. Multiplies quantity and unit price
    C. Adds numbers
    D. Declares variables
  3. What will be the output if quantity = 2 and unit price = 100?
    A. 50
    B. 200
    C. 102
    D. 100
  4. Why are parameters useful in methods?
    A. They make code longer
    B. They reduce flexibility
    C. They allow dynamic data input
    D. They remove variables
  5. Which scenario best demonstrates the use of parameters?
    A. Printing a fixed message
    B. Calculating total price based on user input
    C. Declaring constants
    D. Writing comments

Exercises, Assessment, and Lab Exam

To strengthen your understanding of methods with parameters, practical exercises are essential. These activities are designed to help you gradually improve your coding skills—from simple enhancements to full system development.

Exercises

  1. Modify the program to display the result with currency format (₱)
  2. Add a discount parameter to compute discounted total price
  3. Validate input to ensure quantity and price are not negative

Assessment Task

  • Create a program that:
    • Accepts product name, quantity, and unit price
    • Computes total price using a method
    • Displays a formatted receipt

Lab Exam Task

  • Develop a Mini Billing System:
    • Accept multiple items
    • Use methods with parameters for each calculation
    • Compute subtotal, tax, and grand total
    • Implement menu-driven interaction
    • Include error handling and input validation

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