Programming Course in C# ¡Free!

Conditional operator, positive & smaller

 Friday, April 05, 2013 published by Exercises C#
Proposed exercise

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

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


Output



Solution


using System;
public class Conditional
{
public static void Main()
{
Console.Write("Enter the first number: ");
int a = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter the second number: ");
int b = Convert.ToInt32(Console.ReadLine());

Console.WriteLine(
a > 0 ? "a is positive" : "a is not positive"
);

Console.WriteLine(
b > 0 ? "b is positive" : "b is not positive"
);

Console.WriteLine(
(a > 0) && (b > 0)? "both are positive" : "not both are positive"
);
}
}