0
0
CProgramBeginner · 2 min read

C Program to Check Voting Eligibility

A C program to check voting eligibility reads the user's age using scanf and uses an if statement to print "Eligible to vote" if age is 18 or above, otherwise "Not eligible to vote".
📋

Examples

Input20
OutputEligible to vote
Input17
OutputNot eligible to vote
Input18
OutputEligible to vote
🧠

How to Think About It

To check voting eligibility, first get the person's age. Then compare the age with 18 using the >= operator. If the age is 18 or more, the person can vote; otherwise, they cannot.
📐

Algorithm

1
Get the user's age as input
2
Check if age is greater than or equal to 18
3
If yes, print "Eligible to vote"
4
Otherwise, print "Not eligible to vote"
💻

Code

c
#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    if (age >= 18) {
        printf("Eligible to vote\n");
    } else {
        printf("Not eligible to vote\n");
    }
    return 0;
}
Output
Enter your age: 20 Eligible to vote
🔍

Dry Run

Let's trace the input age 20 through the code

1

Input age

User enters age = 20

2

Check age >= 18

20 >= 18 is true

3

Print result

Print "Eligible to vote"

AgeCondition (age >= 18)Output
20trueEligible to vote
💡

Why This Works

Step 1: Reading input

The program uses scanf to get the user's age from the keyboard.

Step 2: Decision making

It uses an if statement to check if the age is 18 or more with age >= 18.

Step 3: Output result

If the condition is true, it prints "Eligible to vote"; otherwise, it prints "Not eligible to vote".

🔄

Alternative Approaches

Using ternary operator
c
#include <stdio.h>

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    age >= 18 ? printf("Eligible to vote\n") : printf("Not eligible to vote\n");
    return 0;
}
This approach uses a shorter syntax with the ternary operator but may be less clear for beginners.
Using function to check eligibility
c
#include <stdio.h>

int isEligible(int age) {
    return age >= 18;
}

int main() {
    int age;
    printf("Enter your age: ");
    scanf("%d", &age);
    if (isEligible(age)) {
        printf("Eligible to vote\n");
    } else {
        printf("Not eligible to vote\n");
    }
    return 0;
}
This approach separates logic into a function, improving readability and reusability.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a single comparison operation, so it runs in constant time O(1).

Space Complexity

Only a few variables are used, so space complexity is constant O(1).

Which Approach is Fastest?

All approaches run in constant time; using a function adds slight overhead but improves code structure.

ApproachTimeSpaceBest For
If-else statementO(1)O(1)Simple and clear logic
Ternary operatorO(1)O(1)Shorter code, less readable for beginners
Function-based checkO(1)O(1)Better code organization and reuse
💡
Always validate that the input age is a positive number before checking eligibility.
⚠️
Beginners often forget to use the address operator & in scanf, causing input errors.