Exercise
Conditional And Boolean
Objective
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.
Write Your C# Exercise
C# Exercise Example
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}");
}
}
More C# Programming Exercises of Basic Data Types
Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.
In this C# exercise, you are asked to write a program that prompts the user for a real number and displays its square root. Additionally, the program must handle any ...
This exercise in C# aims to develop a program that asks the user for three letters and displays them in reverse order. The user will input one letter at...
This exercise in C# aims to develop a program that prompts the user for a symbol and a width, and then displays a decreasing triangle of the spec...
This exercise in C# aims to develop a program that asks the user for their username and password (both as strings) and repeats the prompt as many...
This C# exercise involves creating a program that prompts the user for their username and password, both as strings. If the entered credentials do not m...
This C# programming exercise requires creating a program that asks the user for two numbers and an operation to perform on them. The supported operations are ...