Basic Calculator (Add, Subtract, Multiply, Divide)

Basic Calculator (Add, Subtract, Multiply, Divide)


Introduction of the Lesson

Operators in programming are like tools that allow us to manipulate data. In C#, operators are used to perform calculations, make comparisons, and control logical decisions. Understanding operators is one of the most important foundations in programming because they allow programs to interact with data in meaningful ways.

In this lesson, we will focus on arithmetic, relational, and logical operators, starting with a practical application: creating a basic calculator in C#. This calculator will allow us to add, subtract, multiply, and divide two numbers. By the end of this activity, you will gain confidence in working with operators and learn how to combine user input, type conversion, and operators to create functional programs.


Objectives (OBE-Oriented)

Before we dive into the code, it’s important to set clear goals. This lesson is designed with four outcome-based objectives:

  • Understand: Explain the role of arithmetic, relational, and logical operators in C#.
  • Learn: Identify the correct C# syntax for performing arithmetic operations.
  • Practice: Build and run a basic calculator program in C# that performs addition, subtraction, multiplication, and division.
  • Apply: Extend the program to handle decision-making, such as checking invalid input (e.g., division by zero) and validating user choices.

These objectives ensure that you not only grasp the concepts but also put them into practice and apply them in realistic coding scenarios.


Beginner-Friendly Source Code & Instructions

Step-by-Step Instructions

  1. Open Visual Studio Code. Make sure the .NET SDK is installed.
    dotnet --version
    
  2. Create a new console project
    dotnet new console -n basic_calculator
    cd basic_calculator
    
  3. Replace Program.cs with the following code:

Program.cs

using System;

namespace BasicCalculatorApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Basic Calculator in C# ===");

            // Step 1: Get first number
            Console.Write("Enter the first number: ");
            double num1 = Convert.ToDouble(Console.ReadLine());

            // Step 2: Get second number
            Console.Write("Enter the second number: ");
            double num2 = Convert.ToDouble(Console.ReadLine());

            // Step 3: Display menu
            Console.WriteLine("\nChoose an operation:");
            Console.WriteLine("1. Addition (+)");
            Console.WriteLine("2. Subtraction (-)");
            Console.WriteLine("3. Multiplication (*)");
            Console.WriteLine("4. Division (/)");

            Console.Write("Enter your choice (1-4): ");
            int choice = Convert.ToInt32(Console.ReadLine());

            double result = 0;

            // Step 4: Perform operation
            if (choice == 1)
            {
                result = num1 + num2;
                Console.WriteLine($"Result: {num1} + {num2} = {result}");
            }
            else if (choice == 2)
            {
                result = num1 - num2;
                Console.WriteLine($"Result: {num1} - {num2} = {result}");
            }
            else if (choice == 3)
            {
                result = num1 * num2;
                Console.WriteLine($"Result: {num1} * {num2} = {result}");
            }
            else if (choice == 4)
            {
                if (num2 != 0)
                {
                    result = num1 / num2;
                    Console.WriteLine($"Result: {num1} / {num2} = {result}");
                }
                else
                {
                    Console.WriteLine("Error: Division by zero is not allowed!");
                }
            }
            else
            {
                Console.WriteLine("Invalid choice. Please select 1–4.");
            }
        }
    }
}

Example Output

=== Basic Calculator in C# ===
Enter the first number: 20
Enter the second number: 5

Choose an operation:
1. Addition (+)
2. Subtraction (-)
3. Multiplication (*)
4. Division (/)
Enter your choice (1-4): 4
Result: 20 / 5 = 4

Summary

In this lesson, we learned how to use arithmetic operators in C# by building a simple calculator program. We practiced reading user input, converting strings to numbers, and applying operators like +, -, *, and /. We also introduced relational and logical operators when handling invalid input, such as division by zero or invalid menu choices. This lesson shows how small programs can be interactive, practical, and logical by combining operators with decision-making structures.


5-Item Multiple Choice (Bloom’s Taxonomy)

  1. Which operator is used for multiplication in C#?
    • A. ×
    • B. *
    • C. x
    • D. mult
  2. If num1 = 10 and num2 = 3, what will num1 % num2 return?
    • A. 3
    • B. 1
    • C. 0
    • D. 10
  3. What happens if you divide a number by zero in C#?
    • A. Program continues normally
    • B. Returns 0
    • C. Causes an error
    • D. Returns infinity
  4. Which of the following is a logical operator in C#?
    • A. +
    • B. &&
    • C. /
    • D. =
  5. In the calculator program, why do we use Convert.ToDouble() instead of Convert.ToInt32()?
    • A. To allow decimal inputs
    • B. To make code shorter
    • C. To avoid syntax errors
    • D. To only accept whole numbers

Exercises, Assessment, and Lab Exam

Introduction to Exercises

To strengthen your understanding of arithmetic and logical operators, practice is essential. These exercises will help you extend the calculator and think of real-world applications where operators are used.


Exercises

  1. Modify the calculator to include modulus (%) operation.
  2. Add an option to calculate the square of a number.
  3. Change the program to accept three numbers instead of two.
  4. Improve the program to handle invalid inputs gracefully (e.g., letters instead of numbers).
  5. Display a message if the result of multiplication is greater than 100 using a relational operator.

Assessment

Performance Task:
Create a calculator that allows the user to:

  • Perform addition, subtraction, multiplication, division, and modulus.
  • Display an error message if the user enters invalid choices or attempts to divide by zero.

Lab Exam

Instruction:
Write a C# console application that:

  • Asks the user for two numbers.
  • Asks the user to choose an operation (add, subtract, multiply, divide).
  • Displays the result.
  • If the user chooses division and the second number is zero, display a custom error message.

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