This is basic - Calculator (Console App) - Compile Me!

Latest

Wednesday, April 24, 2019

This is basic - Calculator (Console App)

This is also a calculator, but works as a console application.



You can enter two numbers along with the operator and get the answer.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Console_Calc
{
    class Program
    {
        static void Main(string[] args)
        {
            double dblFNumber = 0;
            double dblSNumber = 0;
            double dblAnswer = 0;
            string strOperator;

            //User input - First number
            Console.Write("Enter the first number: ");
            dblFNumber = Convert.ToDouble(Console.ReadLine());

            //User input - Operator
            Console.Write("Select the operator ( + , - , x , / ) :");
            strOperator = Console.ReadLine();

            //User input - Second number
            Console.Write("Enter the second number: ");
            dblSNumber = Convert.ToDouble(Console.ReadLine());

            //Calculate
            switch (strOperator)
            {
                case "x":
                    dblAnswer = dblFNumber * dblSNumber;
                    break;
                case "/":
                    dblAnswer = dblFNumber / dblSNumber;
                    break;
                case "+":
                    dblAnswer = dblFNumber + dblSNumber;
                    break;
                case "-":
                    dblAnswer = dblFNumber - dblSNumber;
                    break;
                default:
                    Console.WriteLine("Invalid Operator!");
                    Console.ReadLine();
                    break;
            }

            //Display answer if the inputs are correct
            if (strOperator.Contains("x") || strOperator.Contains("/") || strOperator.Contains("+") || strOperator.Contains("-"))
            {
                Console.WriteLine("Answer : " + dblFNumber + " " + strOperator + " " + dblSNumber + " = " + dblAnswer);
                Console.ReadLine();
            }

            Console.WriteLine("Compile Me!\nPress Enter To Exit...");
            Console.ReadLine();
        }

    }
}


Download source code using this link!

No comments:

Post a Comment