Coders Packet

Basic Calculator in C#(Sharp)

By ITIKELA CHIRANMAI

Helps the user in performing the basic arithmetic operation. And also helps in performing arithmetic operations without error.

Helps in performing Basic arithmetic operations. Many people make mistakes in performing some basic arithmetic operations. In such cases computer gives more accurate results and this code also helps students or small children in understanding and performing basic arithmetic operations.

This program is coded with the help of c sharp programming language which is easy to understand and easy to perform the operations using this code and this also throws some error exceptions when there is a mistake in performing the operations in the code.

This program can be run in an online compiler and in vs code also.

 

 

using System;

class Calculator
{
    static void Main()
    {
        Console.WriteLine("Basic Calculator using C#");
        Console.WriteLine("**************************");

        while (true)
        {
            Console.WriteLine("Enter an operator (add(+), sub(-), mul(*), div(/)) or 'x' to exit:");
            string operatorInput = Console.ReadLine();

            if (operatorInput == "x")
                break;

            Console.WriteLine("Enter the first number:");
            double num1 = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("Enter the second number:");
            double num2 = Convert.ToDouble(Console.ReadLine());

            double result = 0;

            switch (operatorInput)
            {
                case "+":
                    result = num1 + num2;
                    break;
                case "-":
                    result = num1 - num2;
                    break;
                case "*":
                    result = num1 * num2;
                    break;
                case "/":
                    if (num2 != 0)
                        result = num1 / num2;
                    else
                        Console.WriteLine("Error: Division by zero.");
                    break;
                default:
                    Console.WriteLine("Error: Invalid operator.");
                    break;
            }

            Console.WriteLine("Result: " + result);
            Console.WriteLine();
        }

        Console.WriteLine("Calculator program has been exited.");
        Console.ReadKey();
    }
}

 

 

OUTPUT:

Basic Calculator using C#
**************************
Enter an operator (add(+), sub(-), mul(*), div(/)) or 'x' to exit:
+
Enter the first number:
6456
Enter the second number:
4654651
Result: 4661107

Enter an operator (add(+), sub(-), mul(*), div(/)) or 'x' to exit:
-
Enter the first number:
465456
Enter the second number:
145641
Result: 319815

Enter an operator (add(+), sub(-), mul(*), div(/)) or 'x' to exit:
x
Calculator program has been exited.

Output

 

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by ITIKELA CHIRANMAI (Chiranmai)

Download packets of source code on Coders Packet