Answer to Question #326395 in C# for Chinara

Question #326395

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#

1
Expert's answer
2022-04-10T05:24:22-0400
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!");
        }
	}
}

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