Ejercicio
Dividir si no es cero
Objetivo
Escriba un programa en C# para pedir al usuario dos números y muestre su división si el segundo número no es cero; de lo contrario, mostrará "No puedo dividir"
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; // Declaring a variable to store the first number entered by the user
double secondNumber; // Declaring a variable to store the second number 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
// Checking if the second number is not zero
if (secondNumber != 0) // If the second number is not zero
{
// Performing the division and displaying the result
Console.WriteLine("The result of division is: {0}", firstNumber / secondNumber); // Printing the division result
}
else // If the second number is zero
{
// Displaying an error message when division by zero is attempted
Console.WriteLine("I cannot divide"); // Printing the error message
}
}
}