Wednesday 3 January 2018

Simple Calculator Program in C#(sharp)

                   Simple Calculator Program in C#(sharp)

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

namespace calculator
{
    class Program
    {
        static void Main(string[] args)
        {    
            //basic Calculator program//
             double Num,result; //
           
            bool isNum1 = double.TryParse(args[0], out Num); // converting string to double if a conversion succeeded then  isNum is true otherwise false
            bool isNum2 = double.TryParse(args[2], out Num);
            if (isNum1 && isNum2) // if isnum1 and isnum2 is numeric value than execute if block otherwise execute else block
            {
                try
                {

                    char operant = Convert.ToChar(args[1]); // convert string to char
                    switch (operant) // using switch block for checking operant
                    { 
                        case '+':
                            result = double.Parse(args[0]) + double.Parse(args[2]); //add two values
                            Console.WriteLine("{0} + {1} = {2}", args[0], args[2], result); // print result
                            break;
                        case '-':
                            result = double.Parse(args[0]) - double.Parse(args[2]); //add two values
                            Console.WriteLine("{0} - {1} = {2}", args[0], args[2], result); // print result
                            break;
                        case '*':
                            result = double.Parse(args[0]) * double.Parse(args[2]);
                            Console.WriteLine("{0} * {1} = {2}", args[0], args[2], result); // print result
                            break;
                        case '/':
                            result = double.Parse(args[0]) / double.Parse(args[2]);
                            Console.WriteLine("{0} * {1} = {2}", args[0], args[2], result); // print result
                            break;
                        default:
                            Console.WriteLine("Wrong operation"); // if user using the(apart from these +,-,*,/) other operant than print the wrong operation
                            break;
                    }
                    }
                   catch (Exception e)
                  {
                    Console.WriteLine("using calculator with the correct values...");  // handling catching exception
                    Console.WriteLine("Error is that " + e);
                    Console.ReadLine();
                 }
                }
          else
            {
                Console.WriteLine("Wrong operation"); // if args[0] and args[2] is not number then execute else block
            
            }
     
       }
    }
}

No comments:

Post a Comment

😍Developer on Weekends #shorts #officememes #developermemes

😍Developer on Weekends #shorts #officememes #developermemes Welcome to the latest viral YouTube shorts meme for developers! 😍Developer on...