Answers for "calculator c# code"

C#
6

c# calculator

using System;

namespace Calculator
{
    class Calculator
    {
        public static double DoOperation(double num1, double num2, string op)
        {
            double result = double.NaN; // Default value is "not-a-number" which we use if an operation, such as division, could result in an error.

            // Use a switch statement to do the math.
            switch (op)
            {
                case "a":
                    result = num1 + num2;
                    break;
                case "s":
                    result = num1 - num2;
                    break;
                case "m":
                    result = num1 * num2;
                    break;
                case "d":
                    // Ask the user to enter a non-zero divisor.
                    if (num2 != 0)
                    {
                        result = num1 / num2;
                    }
                    break;
                // Return text for an incorrect option entry.
                default:
                    break;
            }
            return result;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            bool endApp = false;
            // Display title as the C# console calculator app.
            Console.WriteLine("Console Calculator in C#r");
            Console.WriteLine("------------------------n");

            while (!endApp)
            {
                // Declare variables and set to empty.
                string numInput1 = "";
                string numInput2 = "";
                double result = 0;

                // Ask the user to type the first number.
                Console.Write("Type a number, and then press Enter: ");
                numInput1 = Console.ReadLine();

                double cleanNum1 = 0;
                while (!double.TryParse(numInput1, out cleanNum1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput1 = Console.ReadLine();
                }

                // Ask the user to type the second number.
                Console.Write("Type another number, and then press Enter: ");
                numInput2 = Console.ReadLine();

                double cleanNum2 = 0;
                while (!double.TryParse(numInput2, out cleanNum2))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput2 = Console.ReadLine();
                }

                // Ask the user to choose an operator.
                Console.WriteLine("Choose an operator from the following list:");
                Console.WriteLine("ta - Add");
                Console.WriteLine("ts - Subtract");
                Console.WriteLine("tm - Multiply");
                Console.WriteLine("td - Divide");
                Console.Write("Your option? ");

                string op = Console.ReadLine();

                try
                {
                    result = Calculator.DoOperation(cleanNum1, cleanNum2, op);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("This operation will result in a mathematical error.n");
                    }
                    else Console.WriteLine("Your result: {0:0.##}n", result);
                }
                catch (Exception e)
                {
                    Console.WriteLine("Oh no! An exception occurred trying to do the math.n - Details: " + e.Message);
                }

                Console.WriteLine("------------------------n");

                // Wait for the user to respond before closing.
                Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "n") endApp = true;

                Console.WriteLine("n"); // Friendly linespacing.
            }
            return;
        }
    }
}
Posted by: Guest on September-25-2020
0

calculator c#

using System;

namespace hello_world
{
    class Program
    {
        static void Main(string[] args)
        {
            int num1;
            string op;
            int num2;
            int answer;

            Console.Write("write number one ... ");
            num1 = Convert.ToInt32(Console.ReadLine());

            Console.Write("Write op ...");
            op = Console.ReadLine();

            Console.Write("write number two ...");
            num2 = Convert.ToInt32(Console.ReadLine());

            switch (op)
            {
                case "+":
                    answer = num1 + num2;
                    Console.WriteLine(answer);
                    break;
                case "-":
                    answer = num1 - num2;
                    Console.WriteLine(answer);
                    break;
                case "*":
                    answer = num1 * num2;
                    Console.WriteLine(answer);
                    break;
                case "/":
                    answer = num1 / num2;
                    Console.WriteLine(answer);
                    break;
                default:
                    Console.WriteLine("Error");
                    break;
            }

            Console.ReadKey();

        }
    }
}
Posted by: Guest on September-09-2021
0

calculator in C#

class Calculator
    {
        protected float num1, num2;
        protected float sum, sub, mul, div;
        protected int choice;
        public Calculator()
        {
            this.num1 = 0;
            this.num2 = 0;
            this.sum = 0;
            this.sub = 0;
            this.mul = 0;
            this.div = 0;
            this.choice = 0;
        }

        public void Main_Menu()
        {
            char y;

            do
            {
                Console.WriteLine("nntttMain Menu");
                Console.WriteLine("nttThis Program is created by Jillani Soft Tech");

                Console.WriteLine("ntPress 1 For Sum");
                Console.WriteLine("tPress 2 For Subtraction");
                Console.WriteLine("tPress 3 For Multiply");
                Console.WriteLine("tPress 4 For Division");
                Console.WriteLine("tPress 5 For Exit");

                Console.Write("ntEnter your Choice! Please Enter Numbers between (1 to 5) : ");
                choice = int.Parse(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        Sum();
                        break;
                    case 2:
                        Sub();
                        break;
                    case 3:
                        Mul();
                        break;
                    case 4:
                        Div();
                        break;
                    default:
                        Console.Write("ntInvalid Input Please Try Again:    <(*?*)>");
                        break;
                }

                Console.WriteLine("ntDo you want to Continue");
                Console.Write("ntPress (Y or y) : ");
                y = char.Parse(Console.ReadLine());



            } while (y == 'Y' || y == 'y');

        }
        private void Sum()
        {
            Console.Write("ntEnter First Number : ");
            num1 = float.Parse(Console.ReadLine());
            Console.Write("ntEnter Second Number : ");
            num2 = float.Parse(Console.ReadLine());

            sum = num1 + num2;

            Console.WriteLine("tThere Sum is this : {0}", sum);
        }
        private void Sub()
        {
            Console.Write("ntEnter First Number : ");
            num1 = float.Parse(Console.ReadLine());
            Console.Write("ntEnter Second Number : ");
            num2 = float.Parse(Console.ReadLine());

            sub = num1 - num2;

            Console.WriteLine("tThere Subtraction is this : {0}", sub);
        }
        private void Mul()
        {
            Console.Write("ntEnter First Number : ");
            num1 = float.Parse(Console.ReadLine());
            Console.Write("ntEnter Second Number : ");
            num2 = float.Parse(Console.ReadLine());

            mul = num1 * num2;

            Console.WriteLine("tThere Multiply is this : {0}", mul);
        }
        private void Div()
        {
            Console.Write("ntEnter First Number : ");
            num1 = float.Parse(Console.ReadLine());
            Console.Write("ntEnter Second Number : ");
            num2 = float.Parse(Console.ReadLine());

            div = num1 / num2;

            Console.WriteLine("tThere Division is this : {0}", div);
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            // Creating Class Object
            Calculator calculator = new Calculator();

            // Calling Class Method
            calculator.Main_Menu();
        }
    }
Posted by: Guest on November-15-2021

C# Answers by Framework

Browse Popular Code Answers by Language