You are currently viewing C Program To Check A Person Is Valid For Vote Or Not Using If-else

C Program To Check A Person Is Valid For Vote Or Not Using If-else

In this tutorial, you will learn how to Write a Program to Check whether a Person is Valid for vote or not by using if-else statement. As we know that a person is valid to vote only if their age is more than 18 years. All the persons whose age is less than 18 years are not valid to vote.

Problem Statement

Our problem statement is to write a C program that checks whether a person is eligible to vote based on their age. Our program will take age as input from the user and then print the output whether that person is valid for a vote or not.

Examples

Enter your age: 25
Output: You are eligible to vote.

Enter your age: 17
Output: You are not eligible to vote.

Algorithm To Check Whether A Person Is Valid For Vote Or Not

  1. Start the program.
  2. Declare a variable age of type int to store the age of the person.
  3. Prompt the user to enter their age by printing the message “Enter your age: “.
  4. Read the age from the user and store it in the age variable using scanf().
  5. Use an if-else statement to check the eligibility for voting:
    • If age is greater than or equal to 18, print the message “You are eligible to vote.”
    • If age is less than 18, print the message “You are not eligible to vote.”
  6. End the program.

Program to check whether a person is valid to vote or not

#include <stdio.h>
int main() {
    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 1

Enter your age: 34
You are eligible to vote

Output 2

Enter your age: 13
You are not eligible to vote

Program Explanation

  • The program starts by including the necessary header file stdio.h.
  • The main() function is defined.
  • Inside the main() function, a variable age of type int is declared to store the age of the person.
  • The program prompts the user to enter their age by printing the message “Enter your age: “.
  • The scanf() function is used to read an integer from the user, and the input is stored in the age variable using the %d format specifier.
  • An if-else statement is used to check the eligibility for voting:
    • If the condition age >= 18 is true, it prints the message “You are eligible to vote.”
    • If the condition is false, it prints the message “You are not eligible to vote.”
  • The return 0 statement ends the program.

Conclusion

This C program allows the user to enter their age and checks whether they are eligible to vote using if-else statements. It checks the age against the voting eligibility criteria and provides the appropriate output based on the age entered by the user.