Conditional And Boolean - C# Programming Exercise

In this C# exercise, you are asked to write a program that uses the conditional operator (also known as the ternary operator) to assign a boolean variable named "bothEven" the value "true" if the two numbers entered by the user are even, or "false" if either of them is odd.

The conditional operator in C# has the following syntax: condition ? value_if_true : value_if_false. In this case, the condition to be evaluated is whether both numbers are even. To check if a number is even, the modulo operator (%) can be used, which returns the remainder of dividing two numbers. If the remainder of dividing by 2 is zero, the number is even.

This exercise is useful for practicing the use of the ternary operator instead of longer conditional structures. It will also help you become familiar with how to perform parity checks and how to assign boolean values based on evaluated conditions. It is a great exercise to understand the syntax and logic of conditional operators in C#.

 Category

Basic Data Types

 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

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}");
    }
}

 Share this C# exercise

 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#.

  •  Exceptions V2

    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 ...

  •  Char

    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...

  •  Triangle

    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...

  •  Password as string

    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...

  •  Password 5 attempts

    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...

  •  Calculator - if

    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 ...