w3resource

C Exercises: Detrermine a specific age is eligible for casting the vote


5. Voting Eligibility

Write a C program to read the age of a candidate and determine whether he is eligible to cast his/her own vote.

Visual Presentation:

Detrermine a specific age is eligible for casting the vote


Sample Solution:

C Code:

#include <stdio.h>   // Include the standard input/output header file.

void main()
{
  int vote_age;   // Declare an integer variable 'vote_age' to store the age of the candidate.

  printf("Input the age of the candidate : ");   // Prompt the user to input the age of the candidate.
  scanf("%d",&vote_age);   // Read and store the user's input in 'vote_age'.

  if (vote_age<18)   // Check if 'vote_age' is less than 18.
  {
    printf("Sorry, You are not eligible to caste your vote.\n");   // Print a message indicating the candidate is not eligible to vote.
    printf("You would be able to caste your vote after %d year.\n",18-vote_age);   // Print the number of years until the candidate is eligible to vote.
  }
  else
    printf("Congratulation! You are eligible for casting your vote.\n");   // Print a message indicating the candidate is eligible to vote.
}

Output:

Input the age of the candidate : 21                                                                           
Congratulation! You are eligible for casting your vote.

Flowchart:

Detrermine a specific age is eligible for casting the vote.


For more Practice: Solve these Related Problems:

  • Write a C program to check voting eligibility and output the number of years remaining if the candidate is underage.
  • Write a C program to validate age for voting and prompt for an identification number if eligible.
  • Write a C program to check voting eligibility using a switch-case structure based on age ranges.
  • Write a C program to determine voting eligibility while also verifying citizenship status from user input.

Go to:


PREV : Leap Year Determination.
NEXT : Signum Function Implementation.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.