Ejercicio
El mayor de tres números
Objetivo
Escriba un programa en C# para obtener tres números del usuario y mostrar el mayor.
Código de Ejemplo
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
double firstNumber, secondNumber, thirdNumber; // Declaring variables to store the three numbers entered by the user
// Asking the user to enter the first number and reading the input
Console.Write("Enter the first number: ");
firstNumber = Convert.ToDouble(Console.ReadLine()); // Converting the input to a double
// Asking the user to enter the second number and reading the input
Console.Write("Enter the second number: ");
secondNumber = Convert.ToDouble(Console.ReadLine()); // Converting the input to a double
// Asking the user to enter the third number and reading the input
Console.Write("Enter the third number: ");
thirdNumber = Convert.ToDouble(Console.ReadLine()); // Converting the input to a double
// Checking which number is the greatest using if-else statements
if (firstNumber >= secondNumber && firstNumber >= thirdNumber) // If the first number is greater than or equal to both other numbers
{
// Displaying the first number as the greatest
Console.WriteLine("The greatest number is: {0}", firstNumber); // Printing the greatest number
}
else if (secondNumber >= firstNumber && secondNumber >= thirdNumber) // If the second number is greater than or equal to both other numbers
{
// Displaying the second number as the greatest
Console.WriteLine("The greatest number is: {0}", secondNumber); // Printing the greatest number
}
else // If the third number is greater than or equal to both other numbers
{
// Displaying the third number as the greatest
Console.WriteLine("The greatest number is: {0}", thirdNumber); // Printing the greatest number
}
}
}