Exercise
One Or Two Negative Numbers
Objective
Write a C# program to prompt the user for two numbers and determine whether both are negative, only one is negative, or neither is negative.
Write Your C# Exercise
C# Exercise Example
using System; // Importing the System namespace to use Console functionalities
class Program
{
// Main method where the program execution begins
static void Main()
{
// Declaring two variables to store the numbers entered by the user
int number1, number2;
// Asking the user to enter the first number
Console.Write("Enter the first number: ");
number1 = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Asking the user to enter the second number
Console.Write("Enter the second number: ");
number2 = Convert.ToInt32(Console.ReadLine()); // Converting the input to an integer
// Checking if both numbers are negative
if (number1 < 0 && number2 < 0) // If both numbers are less than 0
{
// Displaying a message if both numbers are negative
Console.WriteLine("Both numbers are negative."); // Informing the user that both numbers are negative
}
// Checking if only the first number is negative
else if (number1 < 0) // If the first number is less than 0
{
// Displaying a message if only the first number is negative
Console.WriteLine("Only the first number is negative."); // Informing the user that only the first number is negative
}
// Checking if only the second number is negative
else if (number2 < 0) // If the second number is less than 0
{
// Displaying a message if only the second number is negative
Console.WriteLine("Only the second number is negative."); // Informing the user that only the second number is negative
}
else
{
// Displaying a message if neither number is negative
Console.WriteLine("Neither number is negative."); // Informing the user that neither of the numbers is negative
}
}
}