Multiplicar Tres Números Con La Entrada Del Usuario En C#

En este ejercicio, escribirás un programa en C# que solicita al usuario introducir tres números y muestra su multiplicación. Este ejercicio te ayudará a practicar el uso de la salida formateada con {0} para insertar valores dinámicamente en una cadena. Además, aprenderás a gestionar la entrada del usuario, realizar multiplicaciones básicas y escribir comentarios en tu código. El programa también te mostrará cómo organizar el código con comentarios útiles, como se especifica en la tarea.

Grupo

Introducción a C#

Objectivo

El objetivo de este ejercicio es escribir un programa en C# que multiplique tres números introducidos por el usuario e imprima el resultado con una salida formateada con {0}. También incluirá un comentario con su nombre y apellidos en la primera línea del código.

Uso de {0} y comentarios. Escriba un programa en C# que solicite al usuario tres números y muestre su multiplicación. La primera línea debe ser un comentario con su nombre y apellidos. Debería verse así:

Ejemplo de ejercicio en C#

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

using System;

namespace MultiplyThreeNumbers
{
    class Program
    {
        // The Main method is where the program execution begins
        static void Main(string[] args)
        {
            // Declare variables to store the numbers
            double number1, number2, number3, result;

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

            // Ask the user to enter the second number
            Console.Write("Enter the second number to multiply: ");
            number2 = Convert.ToDouble(Console.ReadLine()); // Read and convert the input to a double

            // Ask the user to enter the third number
            Console.Write("Enter the third number to multiply: ");
            number3 = Convert.ToDouble(Console.ReadLine()); // Read and convert the input to a double

            // Perform the multiplication
            result = number1 * number2 * number3;

            // Display the result using formatted output with {0}
            Console.WriteLine("The result of multiplying {0}, {1}, and {2} is: {3}", number1, number2, number3, result);

            // 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 to multiply: 12
Enter the second number to multiply: 23
Enter the third number to multiply: 2
The result of multiplying 12, 23, and 2 is: 552

Comparte este ejercicio de C#