Write a C# Sharp program that takes four numbers as input to calculate and print the average.
C# Sharp: Average of numbers
An average is the sum of a list of numbers divided by the number of numbers in the list. In mathematics and statistics, this would be called the arithmetic mean. The term "arithmetic mean" is preferred in some contexts in mathematics and statistics because it helps distinguish it from other means, such as the geometric mean and the harmonic mean.
Method:
Average is found by taking input from the user into different variables. These variables are then added. Then theresult is divided by the number of numbers ie. Total values are counted and assigned to a different variable or used directly. The sum of numbers is then divided by the count of numbers.
Psuedocode:
Here is the psuedocode
define average(a,b,c,d)
Average = a+b+c+d/4
Sample solution:
C# Code:
using System;
using System.IO;
public class Exercise9 { public static void Main()
{
double number1,number2,number3,number4;
Console.Write("Enter the First number: ");
number1 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter the Second number: ");
number2 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter the third number: ");
number3 = Convert.ToDouble(Console.ReadLine()); Console.Write("Enter the fourth number: ");
number4 = Convert.ToDouble(Console.ReadLine());
double result = (number1 + number2 + number3 + number4) / 4;
Console.WriteLine("The average of {0}, {1}, {2}, {3} is: {4} ", number1, number2, number3, number4, result);
}
}
Sample Output:
Enter first number: 17
Enter second number: 18
Enter third number: 20
Enter fourth number: 25
The average of 17,18,20,25 is : 20
Flowchart:
Here is the flowchart to find the average of given numbers
![]()
![]()


No comments:
Post a Comment