C program to check eligibility for voting

In this program, we will take a person’s age and check it’s eligibility for voting. In India, the minimum age for voting is 18 years. So, we need to check whether the person’s age is greater than or equal to 18. For this, we will use the conditional statements- if and else.

Note: If are from a different country other than India, you simply need to replace 18 with the minimum voting age in your country.

To understand this example you should have the basic knowledge of following topics in C-

#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.

Check this out – C program to find the election winner

Leave a Comment

Your email address will not be published. Required fields are marked *