Write a C program to check whether a user is eligible to vote or not
Description: Take the age of a person as input. If the age is 18 or more, print that the person can vote.
Hint:
- Use the
>=operator withif else.
C program to check the voting eligibility
#include <stdio.h>
int main(void) {
int age;
printf("Enter your age: ");
scanf("%d", &age);
if (age >= 18) {
printf("You are eligible to vote\n");
} else {
printf("You are not eligible to vote\n");
}
return 0;
}
Output:
Output of the above program is
Enter your age: 17
You are not eligible to vote
How it works
- The condition
age >= 18is false for 17, so theelseblock runs.
Read C If Else Statement.
Learn more about the similar topics:
| Tutorials |
|---|
| C If Else Statement |
| C Else If Ladder and Nested If |
| C Switch Statement |
| C While Loop |
| C Do While Loop |
| C For Loop |
| C Nested Loops |