Ejercicio
Multiplicar si no es cero
Objetivo
Escriba un programa en C# para pedir al usuario un número; si no es cero, entonces pedirá un segundo número y mostrará su valor; de lo contrario, mostrará "0".
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()
{
int firstNumber; // Declaring a variable to store the first number entered by the user
int 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 a number: ");
firstNumber = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Checking if the first number is not zero
if (firstNumber != 0) // If the first number is not zero
{
// Asking the user to enter the second number and reading the input
Console.Write("Enter a second number: ");
secondNumber = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Displaying the sum of the two numbers
Console.WriteLine("The sum is: {0}", firstNumber + secondNumber); // Printing the sum
}
else // If the first number is zero
{
// Displaying "0" if the first number is zero
Console.WriteLine("0"); // Printing "0"
}
}
}