C program to check eligibility for voting

In this tutorial, we’ll create a simple C program to check eligibility for voting. This is a beginner-level program that demonstrates the use of if-else conditional statements in C.

In India, the minimum age required for voting is 18. Therefore, this program checks whether a person is eligible to vote based on their age input.

Note: If you’re from a country with a different voting age, simply replace 18 with your country’s minimum voting age.

Prerequisites to Understand This Program

To fully understand this example, you should be familiar with:

  • Basic input and output functions in C (printf, scanf)

  • Conditional statements (if, else)

#include<stdio.h>
int main()
{
    int age;
    printf("Enter your age");
    scanf("%d", &age);
    if(age >= 18)
    {
        printf("You can vote");
    }
    else
    {
        printf("You cannot vote");
    }
    return 0;
}

 Output:

Enter your age21
You can vote

Explanation

  • Here inside the main function, we have declared a variable named ‘age’. In this variable we will store the age entered by the user.
  • Next we have written a message that says – “Enter your age”. When this program runs, this is the first thing the user will see.
  • When the user enter a number and press enter, the value will get stored in the variable ‘age’. We have done this by the use of  ‘scanf’ function.
  • After that we have put if and else statements to check the age of the user. Inside the if statement, we have put a condition that will check if the age is either greater than or equal to 18. If this condition is true, the printf function inside the if statement will get executed and prints the message – “You can vote”. If the condition inside the if statement is false (i.e. the person’s age is less than 18), the else statement would be executed and the message – “You cannot vote” would be printed.

Final Note

This is a great example to learn conditional logic in C programming. Feel free to modify the age value to fit your country’s legal voting age.

If you’re learning C, try creating similar programs like:

  • C program to check if a number is positive or negative

  • C program to determine grade based on marks

Check this out – C program to find the election winner