Answer to Question #239180 in C# for Gayatri

Question #239180

Q1) write generic functions to add, multiply, subtract and divide any kind of data type’s integer, double, decimal and date types.



Q2) write a generic class to implement same above functionalities in your program.


1
Expert's answer
2021-09-19T10:52:15-0400


using System;
using System.Collections.Generic;
namespace App
{
    public class ArithmeticOperations<T>
    {
        private T number1;
        private T number2;


        public ArithmeticOperations()
        {


        }
        
        public ArithmeticOperations(T number1,T number2)
        {
            this.number1=number1;
            this.number2=number2;
        }


        
        public T add(){
            dynamic a = number1;
            dynamic b = number2;
            return a + b;
        }
        public T subtract()
        {
            dynamic a = number1;
            dynamic b = number2;
            return a - b;
        }
        public T multiply()
        {
            dynamic a = number1;
            dynamic b = number2;
            return a * b;
        }
        public T divide()
        {
            dynamic a = number1;
            dynamic b = number2;
            return a / b;
        }
    }


    class Program
    {
        static void Main(string[] args)
        {


            ArithmeticOperations<int> arithmeticOperationsInteger = new ArithmeticOperations<int>(4, 2);
            Console.WriteLine("Integer numbers (4 and 2)");
            Console.WriteLine("Sum: {0}", arithmeticOperationsInteger.add());
            Console.WriteLine("Difference: {0}", arithmeticOperationsInteger.subtract());
            Console.WriteLine("Product: {0}", arithmeticOperationsInteger.multiply());
            Console.WriteLine("Quotient: {0}", arithmeticOperationsInteger.divide());


            ArithmeticOperations<double> arithmeticOperationsDouble = new ArithmeticOperations<double>(4.2, 2.2);
            Console.WriteLine("Double numbers (4.2 and 2.2)");
            Console.WriteLine("Sum: {0}", arithmeticOperationsDouble.add());
            Console.WriteLine("Difference: {0}", arithmeticOperationsDouble.subtract());
            Console.WriteLine("Product: {0}", arithmeticOperationsDouble.multiply());
            Console.WriteLine("Quotient: {0}", arithmeticOperationsDouble.divide());


            ArithmeticOperations<decimal> arithmeticOperationsDecimal = new ArithmeticOperations<decimal>(4.2M, 2.2M);
            Console.WriteLine("Decimal numbers (4.2 and 2.2)");
            Console.WriteLine("Sum: {0}", arithmeticOperationsDecimal.add());
            Console.WriteLine("Difference: {0}", arithmeticOperationsDecimal.subtract());
            Console.WriteLine("Product: {0}", arithmeticOperationsDecimal.multiply());
            Console.WriteLine("Quotient: {0}", arithmeticOperationsDecimal.divide());


           




            


            Console.ReadLine();
        }
    }
}

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