Check if a User is Eligible to Vote in CSharp

Check if a User is Eligible to Vote in C#


Introduction of the Lesson

Programming often involves making decisions based on certain conditions. One of the most practical examples is checking if a person is eligible to vote. In real life, voting eligibility is often determined by age—for example, in many countries, a person must be 18 years old or older to vote.

In C#, we can use relational operators (>=, <=, <, >) and logical operators (&&, ||, !) to create simple decision-making programs. These operators allow us to compare values and execute specific code depending on the outcome. For this lesson, we will use the if-else statement combined with a relational operator to determine if the user is old enough to vote.

This exercise introduces a real-world style program that students can easily relate to. It also strengthens their understanding of how operators are used in combination with conditional logic to create interactive and meaningful programs.


Objectives (OBE-Oriented)

Before we start coding, let’s define what students will gain from this lesson. We’ll focus on four outcomes: understand, learn, practice, and apply.

  • Understand: Explain how relational and logical operators work in C# to evaluate conditions.
  • Learn: Demonstrate how to use the if-else statement to check if a user meets a given requirement.
  • Practice: Create a program that asks for the user’s age and determines if they are eligible to vote.
  • Apply: Extend the concept to real-world style validations, such as checking multiple eligibility requirements (e.g., age and citizenship).

By achieving these objectives, students will not only strengthen their grasp of operators but also see how programming connects directly to solving everyday problems.


Beginner-Friendly Source Code & Instructions

Step-by-Step Instructions

  1. Open Visual Studio Code.
  2. Make sure the .NET SDK is installed. Check with:
    dotnet --version
    
  3. Create a new console project:
    dotnet new console -n vote_checker
    cd vote_checker
    
  4. Open the file Program.cs and replace its content with the code below.
  5. Save the file and run the program:
    dotnet run
    

Program.cs

using System;

namespace VoteEligibilityApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("=== Voter Eligibility Checker ===");

            // Step 1: Ask for user's age
            Console.Write("Enter your age: ");
            int age = Convert.ToInt32(Console.ReadLine());

            // Step 2: Check eligibility
            if (age >= 18)
            {
                Console.WriteLine("You are eligible to vote!");
            }
            else
            {
                Console.WriteLine("Sorry, you are not eligible to vote yet.");
            }
        }
    }
}

Example Outputs

Case 1 – Eligible voter

=== Voter Eligibility Checker ===
Enter your age: 20
You are eligible to vote!

Case 2 – Not eligible voter

=== Voter Eligibility Checker ===
Enter your age: 16
Sorry, you are not eligible to vote yet.

Explanation of the Program

  1. The program begins by importing the System namespace so we can use built-in classes like Console.
  2. Inside the Main method, the program asks the user to input their age. Since Console.ReadLine() accepts strings, we convert the input into an integer with Convert.ToInt32().
  3. We then use the relational operator >= to check if the age is greater than or equal to 18.
  4. If the condition is true, the program displays: “You are eligible to vote!”. Otherwise, it displays: “Sorry, you are not eligible to vote yet.”

This small program demonstrates how relational operators and if-else statements work together to make logical decisions.


Summary

In this lesson, you learned how to create a program that checks if a user is eligible to vote using relational operators in C#. The exercise reinforced your understanding of how to accept input, convert data types, and apply conditions in decision-making. By practicing this, you not only improved your coding skills but also saw how programming can model real-world rules and processes.


5-Item Multiple Choice (Bloom’s Taxonomy)

  1. Which relational operator is used to check if a number is greater than or equal to another?
    • A. <
    • B. <=
    • C. >=
    • D. ==
  2. What will happen if the user inputs age = 18 in the program?
    • A. The program shows “Not eligible to vote.”
    • B. The program shows “You are eligible to vote!”
    • C. Error occurs
    • D. Nothing displays
  3. Why do we need Convert.ToInt32() in this program?
    • A. To compare strings
    • B. To convert input from string to integer
    • C. To convert integers to strings
    • D. To end the program
  4. If the input is 15, which branch of the if-else executes?
    • A. if block
    • B. else block
    • C. Both blocks
    • D. None
  5. Why is this program a good beginner example?
    • A. It shows how to use loops
    • B. It introduces relational operators and condition checking
    • C. It teaches string concatenation
    • D. It uses arrays

Exercises, Assessment, and Lab Exam

Intro to Exercises

Now that you know how to check voting eligibility with a simple condition, let’s expand your skills with practice problems. These tasks will challenge you to extend the logic and apply different operators while reinforcing your learning.


Exercises

  1. Modify the program to ask for both age and citizenship (yes/no). A person can only vote if they are 18+ and a citizen.
  2. Add logic to display how many years are left until the person becomes eligible to vote.
  3. Write a program that asks for multiple ages (e.g., 5 inputs) and checks which users are eligible.
  4. Create a program that categorizes voters: “First-time voter” (age 18), “Regular voter” (19–59), “Senior voter” (60+).
  5. Combine the voting eligibility check with even/odd checking of age for practice.

Assessment

Performance Task:
Write a C# program that:

  • Asks for the user’s age.
  • Displays whether they are eligible to vote.
  • If not eligible, display how many years remain until they can vote.

Lab Exam

Instruction:
Create a console program in C# that:

  1. Accepts user input for age.
  2. Uses relational operators to determine voting eligibility.
  3. Displays appropriate messages based on conditions.
  4. Handles edge cases (e.g., if user enters negative values).

You may visit our Facebook page for more information, inquiries, and comments. Please subscribe also to our YouTube Channel to receive free capstone projects resources and computer programming tutorials.

Hire our team to do the project.

, , , , , , , , , ,

Post navigation