adspace
Write a console application and implement the ternary operator to decide whether the
age a user entered after being prompted is allowed to vote or not(given that only citizens
between 18 and 120 years only inclusive can vote). Use exception handling for non-numerical
input.
Answer Posted / Ajay Dangi
Here's an example in C#:
```csharp
using System;
namespace AgeValidator
{
class Program
{
static void Main(string[] args)
{
try
{
Console.Write("Enter your age: ");
string input = Console.ReadLine();
int age = Convert.ToInt32(input);
if (age >= 18 && age <= 120)
Console.WriteLine("You are eligible to vote.");
else
Console.WriteLine("You are not eligible to vote.");
}
catch (FormatException ex)
{
Console.WriteLine("Invalid input. Please enter a valid number.");
}
}
}
}
```
| Is This Answer Correct ? | 0 Yes | 0 No |
Post New Answer View All Answers