Answer to Question #187779 in C# for CHANDRASENA REDDY

Question #187779

Amit wants to develop an Arithmetic Calculator to perform some arithmetic functions. Develop a program to achieve the same.


Task 1: Create a library project to define a class ArithmeticOperations to perform the operations like Add, Subtract, Multiply, Divide and Modulus on two numbers of integer type, and double type. Use the class in a console application. Accept the details from the user and perform the operation based on user’s choice


1
Expert's answer
2021-05-01T08:12:12-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace C_SHARP
{
    class Program
    {


        class ArithmeticOperations
        {
            public double Add(double number1, double number2)
            {
                return number1 + number2;
            }
            public double Subtract(double number1, double number2)
            {
                return number1 - number2;
            }
            public double Multiply(double number1, double number2)
            {
                return number1 * number2;
            }
            public double Divide(double number1, double number2)
            {
                return number1 / number2;
            }
            public int Modulus(double number1, double number2)
            {
                return (int)(number1 % number2);
            }
        }


        static void Main(string[] args)
        {
            string ch = "";
            ArithmeticOperations arithmeticOperations = new ArithmeticOperations();
            while (!ch.Equals("5"))
            {
                Console.WriteLine("1. Add");
                Console.WriteLine("2. Subtract");
                Console.WriteLine("3. Multiply");
                Console.WriteLine("4. Divide");
                Console.WriteLine("5. Modulus");
                Console.WriteLine("6. Exit");
                Console.Write("Select the operation: ");
                ch = Console.ReadLine();
                double number1=0;
                double number2=0;
                if (ch.Equals("1") || ch.Equals("2") || ch.Equals("3")||
                    ch.Equals("4") || ch.Equals("5") || ch.Equals("6"))
                {
                    Console.Write("Enter the first number: ");
                    number1 = double.Parse(Console.ReadLine());
                    Console.Write("Enter the second number: ");
                    number2 = double.Parse(Console.ReadLine());
                }
                if (ch.Equals("1"))
                {
                    
                    Console.WriteLine("{0} + {1} = {2}", number1, number2, arithmeticOperations.Add(number1, number2));
                }
                else if (ch.Equals("2"))
                {
                    Console.WriteLine("{0} - {1} = {2}", number1, number2, arithmeticOperations.Subtract(number1, number2));
                }
                else if (ch.Equals("3"))
                {
                    Console.WriteLine("{0} * {1} = {2}", number1, number2, arithmeticOperations.Multiply(number1, number2));
                }
                else if (ch.Equals("4"))
                {
                    Console.WriteLine("{0} / {1} = {2}", number1, number2, arithmeticOperations.Divide(number1, number2));
                }
                else if (ch.Equals("5"))
                {
                    Console.WriteLine("{0} % {1} = {2}", number1, number2, arithmeticOperations.Modulus(number1, number2));
                }
                else if (ch.Equals("6"))
                {
                    //exit
                }
                else
                {
                    Console.WriteLine("\nSelect correct menu item.\n");
                }
            }
        }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS