- Course
- Data types B
- Conditional operator and boolean
Conditional operator and boolean
Last updated:
8/23/2020
⁃
Difficulty:
Easy
Create a C# program that uses the conditional operator to assign value to a boolean
variable. You must assign true if two numbers entered by the user (x, y) are even and false if any of them is odd.
Finally, it shows the value of the variable on the screen.
Input
2
2
Output
True
Solution
using System;
public class ConditionalOperatorAndBoolean
{
public static void Main(string[] args)
{
int x = Convert.ToInt32(Console.ReadLine());
int y = Convert.ToInt32(Console.ReadLine());
bool both = ((x % 2 == 0) && (y % 2 == 0)) ? true : false;
Console.WriteLine(both);
}
}