Grupo
Introducción a C#
Objectivo
El objetivo de este ejercicio es desarrollar un programa en C# que solicite al usuario ingresar tres números (a, b y c), calcule las dos expresiones (a+b)⋅c y a⋅c+b⋅c, y muestre los resultados en un formato estructurado.
Escriba un programa en C# que solicite al usuario tres números (a, b, c) y muestre el resultado de (a + b) · c y el resultado de a · c + b · c.
Ejemplo de ejercicio en C#
Mostrar código C#
// First and Last Name: John Doe
using System;
namespace ArithmeticOperations
{
class Program
{
// The Main method is where the program execution begins
static void Main(string[] args)
{
// Declare variables to store the three numbers
double a, b, c;
double result1, result2;
// Prompt the user to enter the first number (a)
Console.Write("Enter the first number (a): ");
a = Convert.ToDouble(Console.ReadLine()); // Read and convert the input to a double
// Prompt the user to enter the second number (b)
Console.Write("Enter the second number (b): ");
b = Convert.ToDouble(Console.ReadLine());
// Prompt the user to enter the third number (c)
Console.Write("Enter the third number (c): ");
c = Convert.ToDouble(Console.ReadLine());
// Calculate the results of the two expressions
result1 = (a + b) * c;
result2 = (a * c) + (b * c);
// Display the results
Console.WriteLine("\nResults:");
Console.WriteLine("({0} + {1}) * {2} = {3}", a, b, c, result1);
Console.WriteLine("{0} * {2} + {1} * {2} = {3}", a, b, c, result2);
// 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 (a): 2
Enter the second number (b): 3
Enter the third number (c): 4
Results:
(2 + 3) * 4 = 20
2 * 4 + 3 * 4 = 20
Código de ejemplo copiado
Comparte este ejercicio de C#