8. Print the average of three numbers entered by the user by creating a class named 'Average' having a function to calculate and print the average without creating any object of the Average class c#
using System;
class Average
{
public static void Calculate(params double [] ps)
{
double sum = 0;
for (int i = 0; i < ps.Length; i++)
sum += ps[i];
Console.WriteLine($"Average: {sum / ps.Length:0.000}");
}
}
class Program
{
static void Main(String[] args)
{
try
{
Console.Write("Enter first number> ");
double A1 = Double.Parse(Console.ReadLine());
Console.Write("Enter second number> ");
double A2 = Double.Parse(Console.ReadLine());
Console.Write("Enter third number> ");
double A3 = Double.Parse(Console.ReadLine());
Average.Calculate(A1, A2, A3);
}catch(Exception ex)
{
Console.Write(ex.Message);
Console.WriteLine(" Try again!");
}
}
}
Comments
Leave a comment