Freelancer

Eligibility to vote

Eligibility to vote - C Programming Example

This C program code will take your age as input and will tell us the eligibility to vote. 

C Programming Example
Voting Eligibility

C Programming Code

#include<stdio.h>
#include<conio.h>
main()
{
    int age,year;
    printf ("Input your age \n");
    scanf("%d",&age);
    if(age<18)
        {
            year= 18-age;
        printf("Sorry, you are not eligilble to vote now \n");
        printf("You have to wait %d more years.", year);

        }
        if (age>=18)
        {
            printf("Congratulations! you are eligible to vote. Choose wisely.");
        }
        getch();

}

Output

C Programming Example
Eligibility to vote
C Programming Example
Eligibility to vote

Explanation

·        #include<stdio.h>
#include<stdio.h>
Ø  This are called header file.A header file is a file with extension .h which contains C function declarations and macro definitions to be shared between several source filesThe C programming language provides many standard library functions for file input and output. These functions make up the bulk of the C standard library header <stdio.h>.In the other hand  <conio.h> is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C. This header declares several useful library functions for performing "console input and output" from a program.
·         main()
Ø  In C, program execution starts from the main() function. The main function can in-turn call other functions. When main calls a function, it passes the execution control to that function. The function returns control to main when a return statement is executed or when end of function is reached.
·         Int age,year;
Ø  int is a variable or data type. A variable declared to be of type int can be used for storing integers.. Here we declare “age” and“year” variable in int type.
·         printf() function
Ø  C uses printf() function to write from the input devices. This function has been declared in the header file called stdio.h . Any text written within the pair of quotes ("") is displayed as such by printf() function on the screen. 
·         scanf() function
Ø  This function is used to get input from the user of the program. scanf(“%d,&age) is used here. So, this program will read in a integer value for “age” variable that the user enters on the keyboard (%d is for integer values. As there is “&age”, the number user enters will be the value of “age” variable).
if(age<18)
        {
            year= 18-age;
        printf("Sorry, you are not eligible to vote now \n");
        printf("You have to wait %d more years.", year);
}
Here if loop is used as a statement where the condition is that the age is below 18 then the program will show Sorry, you are not eligible to vote now and then as year was previously declared for this loop only that year=18-age then the program will show how many years the person has to wait.
Remember every loop has to have its separate {} or else the loop wont run as the beginning and end isn’t designated.
        if (age>=18)
        {
            printf("Congratulations! you are eligible to vote. Choose wisely.");
        }

For this loop the condition is set the program will show what is written in printf.
Then {} ends and the second loop also ends.
Basically 2 loops are included in this program because, if the description fits one condition then it will show one output and if it fits the other condition it will show the other output.
getch();
Finally getch(); is written to end the program.



No comments

Powered by Blogger.