Complementary task for topic: 2

M Nemeth · 2023-08-29 15:21:04.606222'

If/else V.

If/else V.

Task: Determine the eligibility for a voting age group:
Write a C program that prompts the user to enter their age and determines their eligibility for voting based on the following age groups:

Age 18-20: Eligible for voting in local elections.
Age 21-65: Eligible for voting in local and national elections.
Age 66 and above: Eligible for voting in local, national, and senior citizen elections.
Age below 18: Not eligible for voting.

Hint: It is simple, but you can simplify the solution

Solution
#include 

int main() {
    int age;
    
    printf("Enter your age: ");
    scanf("%d", &age);
    
    if (age >= 18) {
        if (age >= 18 && age <= 20) {
            printf("Your age is %d. You are eligible for voting in local elections.\n", age);
        } else if (age >= 21 && age <= 65) {
            printf("Your age is %d. You are eligible for voting in local and national elections.\n", age);
        } else {
            printf("Your age is %d. You are eligible for voting in local, national, and senior citizen elections.\n", age);
        }
    } else {
        printf("Your age is %d. You are not eligible for voting.\n", age);
    }
    
    return 0;
}



Explanation
In this example, the program uses printf() to prompt the user to enter their age. The scanf() function is used to scan the input from the user and store it in the age variable. The program uses nested if/else statements to check the value of age and determine the corresponding election eligibility using printf().
< < previous    next > >