Show the Result of a Math Operation in C#
Table of Contents
- Show the Result of a Math Operation in C#
- Introduction
- Objectives
- Beginner-Friendly Source Code & Step-by-Step Instructions
- Step 1: Open VS Code and Terminal
- Step 2: Create a new console project
- Step 3: Replace Program.cs with the code below
- Step 4: Run the program
- Example Output
- Summary
- Multiple Choice Quiz
- Exercises, Assessment & Lab Exam
- Intro
- Exercises
- Assessment
- Lab Exam
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.
- Understand – Explain how arithmetic operators work in C#.
- Learn – Identify and use correct C# syntax for sum, difference, product, and quotient.
- Practice – Write and run a C# console program that performs math operations on two numbers.
- 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
- Which symbol is used for multiplication in C#?
A. ×
B. *
C. x
D. mul - What will be the output of
20 / 5
in C#?
A. 2
B. 4
C. 5
D. 10 - If
num1 = 15
andnum2 = 10
, what is the result ofnum1 - num2
?
A. 25
B. 5
C. -5
D. 0 - Which operator is used for addition?
A. –
B. *
C. +
D. / - 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
- Change the numbers
num1
andnum2
to new values and observe the results. - Add a modulus operation (
%
) to find the remainder of the division. - Allow the user to input two numbers instead of hardcoding them.
- Display the results in a sentence format, e.g., “The sum of 20 and 5 is 25.”
- 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.