2.33 - Conditional operator

Objetive
Create a program which assigns a integer variable "amountOfPositives" the value 0, 1 or 2, depending on the values of two numbers a & b (entered by the user).

Do it in two different ways: first using "if" and then using the "conditional operator" (?)


Solution
using System;
public class Exercise052
{
    public static void Main()
    {
        int a, b, amountOfPositives;
        
        Console.Write("Enter 1st number: ");
        a = Convert.ToInt32(Console.ReadLine());
        Console.Write("Enter 2nd number: ");
        b = Convert.ToInt32(Console.ReadLine());
        
        if ((a > 0) && (b > 0))
            amountOfPositives = 2;
        else
            if ((a > 0) || (b > 0))
                amountOfPositives = 1;
            else
                amountOfPositives = 0;
        Console.WriteLine("{0} positives.", amountOfPositives);
        
        amountOfPositives = ((a > 0) && (b > 0)) ? 2 :
            ((a > 0) || (b > 0)) ? 1 : 0;
        Console.WriteLine("{0} positives.", amountOfPositives);
    }
}

Popular Posts