Exercise
Conditional and boolean
Objetive
Write a C# program that uses the conditional operator to give a boolean variable named "bothEven" the value "true" if two numbers entered by the user are the even, or "false" if any of them is odd.
Example Code
using System; // Import the System namespace for basic functionality
class Program // Define the main class
{
static void Main() // The entry point of the program
{
// Ask the user to enter two numbers
Console.Write("Enter the first number: ");
int num1 = int.Parse(Console.ReadLine()); // Read and convert the first input to an integer
Console.Write("Enter the second number: ");
int num2 = int.Parse(Console.ReadLine()); // Read and convert the second input to an integer
// Use the conditional operator to check if both numbers are even
bool bothEven = (num1 % 2 == 0 && num2 % 2 == 0) ? true : false;
// Display the result
Console.WriteLine($"Both numbers are even: {bothEven}");
}
}