Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CSHARP

c# calculator

//A simple calculator guide for total c# begginers




//prompts and gathering data
                Console.Write("Enter a number: ");
                int N1 = Convert.ToInt32(Console.ReadLine());

                Console.Write("Enter an operator: ");
                string op = Console.ReadLine();

                Console.Write("Enter a number: ");
                int N2 = Convert.ToInt32(Console.ReadLine());

                //if statements
                if (op == "+")
                {
                    Console.WriteLine(N1 + N2);
                }else if (op == "-")
                {
                    Console.WriteLine(N1 - N2);
                }else if (op == "*")
                {
                    Console.WriteLine(N1 * N2);
                }else if (op == "/")
                {
                    Console.WriteLine(N1 / N2);
                }
                else
                {
                    //if not stated operator, say "error"
                    Console.WriteLine("Error");
                }
            }
            catch
            {
                //if other problem spotted, say "error
                Console.Write("Error");
            }
 
PREVIOUS NEXT
Tagged: #calculator
ADD COMMENT
Topic
Name
4+8 =