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


Please Help Members By Posting Answers For Below Questions

How do you inherit a class into other class in c#?

995


What is expression tree in c#?

998


Why can't we use a static class instead of singleton?

954


Which namespaces are necessary to create a localized application?

1142


How to assign Null value to Var?

1064


What is an abstract class c#?

969