w3resource

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

C Conditional Statement: Exercise-5 with Solution

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

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

C Programming Code Editor:

Previous: Write a C program to find whether a given year is a leap year or not.
Next: Write a C program to read the value of an integer m and display the value of n is 1 when m is larger than 0, 0 when m is 0 and -1 when m is less than 0.

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.