Here’s an example of a C program that checks whether a person is eligible to vote or not:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #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; } |
This program uses the C standard library and specifically stdio.h
and uses input and output functions. Here’s a step by step explanation of what the code does:
- The program starts by including the
stdio.h
header, which provides access to the standard input and output functions in C such asprintf
andscanf
. - The
main()
function is the entry point of the program. Inside the main function, it declares an integer variableage
. - It uses
printf
function to prompt the user to enter their age andscanf
function to read the user input and store it in the variableage
. - Next, it uses an
if-else
statement to check if the value of theage
variable is greater than or equal to 18. If the condition is true, the program prints a message “You are eligible to vote.” to indicate that the person is eligible to vote. - If the condition is false, the program prints a message “You are not eligible to vote.” to indicate that the person is not eligible to vote.
- The
return 0
statement at the end of the main function is used to indicate that the program has executed successfully.
This program prompts the user to enter their age and uses an if-else statement to check if the person is eligible to vote or not based on their age. It will give the output as per the condition written in the code i.e if the entered age is greater than or equal to 18, it will give the message ‘You are eligible to vote’ otherwise ‘You are not eligible to vote’.
It is important to note that different countries have different voting age requirement, in most countries 18 is the age limit but this may not be the case in every country.
The output of this program will be a string of text displayed on the console, depending on the input provided by the user.
When the program is executed, it will prompt the user to enter their age. For example, if the user enters 20, the output will be:
1 2 3 4 | Enter your age: 20 You are eligible to vote. |
And if the user enters 15, the output will be:
1 2 3 4 | Enter your age: 15 You are not eligible to vote. |
It’s worth noting that the output of this program solely depends on the input provided by the user.