fork download
  1. using System;
  2.  
  3. public class VotingEligibility
  4. {
  5. public static void Main(string[] args)
  6. {
  7. // Define the minimum voting age
  8. int votingAge = 18;
  9.  
  10. // Prompt the user to enter their age
  11. Console.Write("Please enter your age: ");
  12.  
  13. // Read the user's input and convert it to an integer
  14. // Use a try-catch block to handle potential invalid input (e.g., non-numeric)
  15. try
  16. {
  17. int age = Convert.ToInt32(Console.ReadLine());
  18.  
  19. // Check if the entered age meets the voting age requirement
  20. if (age >= votingAge)
  21. {
  22. Console.WriteLine("Congratulations! You are old enough to vote.");
  23. }
  24. else
  25. {
  26. int yearsRemaining = votingAge - age;
  27. Console.WriteLine($"Sorry, you are not old enough to vote. You can vote in {yearsRemaining} year(s).");
  28. }
  29. }
  30. catch (FormatException)
  31. {
  32. Console.WriteLine("Invalid input. Please enter a valid number for your age.");
  33. }
  34. catch (OverflowException)
  35. {
  36. Console.WriteLine("The entered age is too large or too small. Please enter a realistic age.");
  37. }
  38. }
  39. }
Success #stdin #stdout 0.06s 29108KB
stdin
Standard input is empty
stdout
Please enter your age: Sorry, you are not old enough to vote. You can vote in 18 year(s).