Group
C# Basic Data Types Overview
Objective
1. Prompt the user to enter two integer numbers.
2. Use the conditional (ternary) operator to check if both numbers are even.
3. Assign the result to a boolean variable named `bothEven`.
4. Display whether both numbers are even or not based on the value of `bothEven`.
5. Ensure proper input handling to avoid errors when entering non-numeric values.
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 even, or "false" if any of them is odd.
Example C# Exercise
Show C# Code
using System;
class Program
{
static void Main()
{
// Prompt user for first number
Console.Write("Enter the first number: ");
int num1 = Convert.ToInt32(Console.ReadLine());
// Prompt user for second number
Console.Write("Enter the second number: ");
int num2 = Convert.ToInt32(Console.ReadLine());
// Use conditional (ternary) 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}");
}
}
Output
//Example 1:
Enter the first number: 8
Enter the second number: 14
Both numbers are even: True
//Example 2:
Enter the first number: 7
Enter the second number: 10
Both numbers are even: False