Conditional Operator, Positive & Smaller - C# Programming Exercise

This C# exercise aims to develop a program that asks the user for two numbers and uses the conditional operator (?) to answer the following questions:
If the first number is positive.
If the second number is positive.
If both numbers are positive.
Which one is the smaller number.
The use of the conditional operator allows evaluating conditions compactly by comparing the two entered numbers. In this case, several expressions using this operator will be used to check the conditions and determine which number is smaller. This exercise is useful for learning how to work with the ternary operator in C# and understanding how it can simplify decision-making logic in a program. Additionally, it provides practice in number comparison and managing conditions in a single step.

 Category

Flow Control

 Exercise

Conditional Operator, Positive & Smaller

 Objective

Write a C# program that asks the user for two numbers and answers, using the conditional operator (?), for the following:

If the first number is positive
If the second number is positive
If both are positive
Which one is smaller

 Write Your C# Exercise

using System;  // Import the System namespace which contains fundamental classes

class Program  // Define the Program class
{
    static void Main()  // The entry point of the program
    {
        // Ask the user to enter the first number
        Console.Write("Enter the first number: ");
        int num1 = int.Parse(Console.ReadLine());  // Read and convert the input into an integer

        // Ask the user to enter the second number
        Console.Write("Enter the second number: ");
        int num2 = int.Parse(Console.ReadLine());  // Read and convert the input into an integer

        // Use the conditional operator to check if the first number is positive
        string result1 = (num1 > 0) ? "The first number is positive." : "The first number is not positive.";  
        Console.WriteLine(result1);  // Display the result for the first number

        // Use the conditional operator to check if the second number is positive
        string result2 = (num2 > 0) ? "The second number is positive." : "The second number is not positive.";  
        Console.WriteLine(result2);  // Display the result for the second number

        // Use the conditional operator to check if both numbers are positive
        string result3 = (num1 > 0 && num2 > 0) ? "Both numbers are positive." : "At least one number is not positive.";  
        Console.WriteLine(result3);  // Display the result if both numbers are positive

        // Use the conditional operator to check which number is smaller
        string smaller = (num1 < num2) ? "The first number is smaller." : (num2 < num1) ? "The second number is smaller." : "Both numbers are equal.";  
        Console.WriteLine(smaller);  // Display the result for the smaller number
    }
}

 Share this C# exercise

 More C# Programming Exercises of Flow Control

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

  •  Prime number

    This C# exercise aims to develop a program that asks the user for an integer and determines if it is a prime number or not. A prime number is one...

  •  Give change

    This exercise in C# aims to develop a program that calculates the change for a purchase, using the largest possible coins or bills. The program s...

  •  Exceptions

    This exercise in C# aims to develop a program that asks the user for two numbers and displays their division. The program should handle potential errors...

  •  Positive and negative

    In this C# exercise, you will learn how to create a program that determines whether a number entered by the user is positive or negative. The program will prom...

  •  Multiply if not zero

    In this C# exercise, you will learn how to create a program that asks the user for a number. If the entered number is not zero, the program will ask for a second number and ...

  •  Divide if not zero

    In this C# exercise, you will learn how to create a program that asks the user for two numbers. If the second number is not zero, the program will perform the division...