Show the Result of a Math Operation in CSharp

Show the Result of a Math Operation in C#


Introduction

Math operations are at the core of every programming language. Whether you’re calculating totals in a store, computing grades in school, or creating financial reports, arithmetic operations like addition, subtraction, multiplication, and division are essential. In this lesson, we will learn how to perform and display the results of these basic operations in C# using simple variables. This activity will strengthen your understanding of operators and how to use them in real-life applications.


Objectives

This lesson follows Outcome-Based Education (OBE).

Intro to Objectives: The aim is not just to perform math operations but also to build problem-solving skills. You will understand the concept, learn how to write the syntax correctly, practice by coding, and apply it to real-world tasks.

  1. Understand – Explain how arithmetic operators work in C#.
  2. Learn – Identify and use correct C# syntax for sum, difference, product, and quotient.
  3. Practice – Write and run a C# console program that performs math operations on two numbers.
  4. Apply – Modify the program to solve other real-world style calculations.

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

Step 1: Open VS Code and Terminal

  • Make sure the .NET SDK is installed (dotnet --version).
  • Create/open your folder (e.g., D:\2025-1_doe_bsis_1a).

Step 2: Create a new console project

dotnet new console -n math_operations
cd math_operations

Step 3: Replace Program.cs with the code below

using System;

namespace MathOperationsApp
{
    class Program
    {
        static void Main(string[] args)
        {
            // Declare two numbers
            int num1 = 20;
            int num2 = 5;

            // Perform operations
            int sum = num1 + num2;
            int difference = num1 - num2;
            int product = num1 * num2;
            int quotient = num1 / num2;

            // Display results
            Console.WriteLine("Math Operations in C#");
            Console.WriteLine("First Number: " + num1);
            Console.WriteLine("Second Number: " + num2);
            Console.WriteLine("Sum: " + sum);
            Console.WriteLine("Difference: " + difference);
            Console.WriteLine("Product: " + product);
            Console.WriteLine("Quotient: " + quotient);
        }
    }
}

Step 4: Run the program

dotnet run

Example Output

Math Operations in C#
First Number: 20
Second Number: 5
Sum: 25
Difference: 15
Product: 100
Quotient: 4

Summary

In this lesson, you learned how to perform basic arithmetic operations in C#. Using two integers, we computed their sum, difference, product, and quotient. These are the foundations of solving more complex mathematical problems in programming. Mastering operators will allow you to apply programming in real-world tasks like accounting systems, grade computations, and business applications.


Multiple Choice Quiz

  1. Which symbol is used for multiplication in C#?
    A. ×
    B. *
    C. x
    D. mul
  2. What will be the output of 20 / 5 in C#?
    A. 2
    B. 4
    C. 5
    D. 10
  3. If num1 = 15 and num2 = 10, what is the result of num1 - num2?
    A. 25
    B. 5
    C. -5
    D. 0
  4. Which operator is used for addition?
    A. –
    B. *
    C. +
    D. /
  5. Why is division by zero not allowed in C#?
    A. It makes the output too large
    B. It results in an undefined value
    C. It multiplies instead
    D. It always gives zero

Exercises, Assessment & Lab Exam

Intro

To strengthen your understanding of arithmetic operators, the following exercises will help you modify and extend the math operations program. These tasks will prepare you for more complex calculations later.

Exercises

  1. Change the numbers num1 and num2 to new values and observe the results.
  2. Add a modulus operation (%) to find the remainder of the division.
  3. Allow the user to input two numbers instead of hardcoding them.
  4. Display the results in a sentence format, e.g., “The sum of 20 and 5 is 25.”
  5. Format the output so each result appears on a new line with labels.

Assessment

  • Quiz: Answer the 5 multiple-choice questions above.
  • Performance Task: Modify the program to accept three numbers and display their total, average, and product.

Lab Exam

  • Write a program in C# that asks the user for two numbers.
  • Compute and display their sum, difference, product, quotient, and remainder.
  • Each result must be shown on a separate line with clear labels.

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