- Course
- Flow controls C
- Conditional operator nested
Conditional operator nested
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that asks the user for two integers (a, b) and check how many of them are positive.
Use a conditional operator nested in C#.
Input
3
2
Output
2
Solution
using System;
public class ConditionalOperatorNested
{
public static void Main(string[] args)
{
int a = Convert.ToInt32(Console.ReadLine());
int b = Convert.ToInt32(Console.ReadLine());
int amount = ((a > 0) && (b > 0)) ? 2 :
((a > 0) || (b > 0)) ? 1 : 0;
Console.WriteLine(amount);
}
}