Calcular El Promedio De Cuatro Números En C#

En este ejercicio, creará un programa en C# que solicita al usuario que introduzca cuatro números y luego calcula su promedio. Este ejercicio está diseñado para fortalecer su comprensión del manejo de la entrada del usuario, las operaciones aritméticas y la salida formateada en C#. Al completar esta tarea, mejorará su capacidad para realizar cálculos numéricos y mostrar resultados estructurados en la consola.

Grupo

Introducción a C#

Objectivo

El objetivo de este ejercicio es desarrollar un programa en C# que solicite al usuario ingresar cuatro números, calcule su promedio y muestre el resultado de forma clara y con formato.

Escriba un programa en C# para calcular y mostrar el promedio de cuatro números ingresados ​​por el usuario.

Ejemplo de ejercicio en C#

 Copiar código C#
// First and Last Name: John Doe

using System;

namespace AverageCalculator
{
    class Program
    {
        // The Main method is where the program execution begins
        static void Main(string[] args)
        {
            // Declare variables to store the four numbers
            double num1, num2, num3, num4, average;

            // Prompt the user to enter the first number
            Console.Write("Enter the first number: ");
            num1 = Convert.ToDouble(Console.ReadLine()); // Read and convert the input to a double

            // Prompt the user to enter the second number
            Console.Write("Enter the second number: ");
            num2 = Convert.ToDouble(Console.ReadLine());

            // Prompt the user to enter the third number
            Console.Write("Enter the third number: ");
            num3 = Convert.ToDouble(Console.ReadLine());

            // Prompt the user to enter the fourth number
            Console.Write("Enter the fourth number: ");
            num4 = Convert.ToDouble(Console.ReadLine());

            // Calculate the average
            average = (num1 + num2 + num3 + num4) / 4;

            // Display the result
            Console.WriteLine("\nThe average of {0}, {1}, {2}, and {3} is: {4}", num1, num2, num3, num4, average);

            // Wait for the user to press a key before closing the console window
            Console.ReadKey(); // This keeps the console window open until a key is pressed
        }
    }
}

 Output

Enter the first number: 10  
Enter the second number: 20  
Enter the third number: 30  
Enter the fourth number: 40  

The average of 10, 20, 30, and 40 is: 25  

Comparte este ejercicio de C#