In this C tutorial we are going to check where a person is eligible to vote or not. There are several ways where we can check this condition but in this tutorial , we will do it using if….else and write C program to check whether the person is eligible to vote or not.
Program Outline
In this C program, we’ll take name of the person as our first input and store it in a variable. Secondly we’ll take age of the person as our second input and store it in another variable. At last, we will check if age of the person is more than or less than 18 using if ….else statement in C and print our conclusion.
Concept
Before writing this C program to check whether the person is eligible to vote or not. You need to learn few concepts before writing this program which are:
The main concept which we are going to use in this program is if…else. First we are going to take person’s age as a input and store it in a variable. Then by using if…else statement we can check whether the person is eligible to vote or not.
If…else statement
The syntax of if else –
if (expression) { // run code if expression is true } else { // run code if expression is false }
Program
After understanding the basic concept required to write this program now let’s begin with the main part and start writing code-
#include<stdio.h>
void main()
{
char a[20];
int b;
//Input Name
printf("Enter the name of the person: ");
scanf("%s",&a);
//Input age
printf("Enter the age of the person: ");
scanf("%d",&b);
//Using if else
if (b>=18)
{
printf("%s is Eligible for voting",&a);
}
else
{
printf("%s is Not Eligible for voting",&a);
}
}
Output
After writing the C program to check whether the person is eligible to vote or not. Let’s check the output what we are getting. As we inputted name as “Harry” and his age is “22” let’s see what output it gives.
