Ejercicio
Positivo y negativo
Objetivo
Escriba un programa en C# para obtener un número y responda si es positivo o negativo.
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 number; // Declaring a variable to store the number entered by the user
// Asking the user to enter a number and reading the input
Console.Write("Enter a number: ");
number = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Checking if the number is positive, negative, or zero
if (number > 0) // If the number is greater than zero
{
Console.WriteLine("The number is positive."); // Printing that the number is positive
}
else if (number < 0) // If the number is less than zero
{
Console.WriteLine("The number is negative."); // Printing that the number is negative
}
else // If the number is neither positive nor negative (i.e., it is zero)
{
Console.WriteLine("The number is zero."); // Printing that the number is zero
}
}
}