Conditional operator
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
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
Input
6
4
Output
A is positive
B is positive
Both are positive
Solution
using System;
public class ConditionalOperator
{
public static void Main(string[] args)
{
int a = Convert.ToInt32(Console.ReadLine());
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");
}
}