107 308
Assignments Done
98.9%
Successfully Done
In March 2023

# 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
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;
}

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("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("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("Difference: {0}", arithmeticOperationsDecimal.subtract());
Console.WriteLine("Product: {0}", arithmeticOperationsDecimal.multiply());
Console.WriteLine("Quotient: {0}", arithmeticOperationsDecimal.divide());

}
}
}

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!