Exercise
Average
Objetive
Write a C# program to calculate and display the average of four numbers entered by the user.
Example Code
using System; // Importing the System namespace to use Console functionalities
// Main class of the program
class Program
{
// Main method where the program execution begins
static void Main()
{
// Declaring four variables to store the numbers entered by the user
double num1, num2, num3, num4;
// Asking the user to enter the first number and reading the input
Console.Write("Enter the first number: ");
num1 = Convert.ToDouble(Console.ReadLine());
// Asking the user to enter the second number and reading the input
Console.Write("Enter the second number: ");
num2 = Convert.ToDouble(Console.ReadLine());
// Asking the user to enter the third number and reading the input
Console.Write("Enter the third number: ");
num3 = Convert.ToDouble(Console.ReadLine());
// Asking the user to enter the fourth number and reading the input
Console.Write("Enter the fourth number: ");
num4 = Convert.ToDouble(Console.ReadLine());
// Calculating the average of the four numbers
double average = (num1 + num2 + num3 + num4) / 4;
// Printing the average to the screen
Console.WriteLine("The average of the four numbers is: " + average);
}
}