Freelancer

Leap year

Leap Year Identifier - C Programming Example

C programing example
Tropical year-Common year-Leap year

C Programming Code

#include<stdio.h>
#include<conio.h>
main()
{
    int year;

    printf("Enter a year: ");
    scanf("%d",&year);

    if(year%4 == 0)
    {
        if( year%100 == 0)
        {
            if ( year%400 == 0)
                printf("%d is a leap year.", year);
            else
                printf("%d is not a leap year.", year);
        }
        else
            printf("%d is a leap year.", year );
    }
    else
        printf("%d is not a leap year.", year);
        getch();

}

Output example

C Programming Example
Leap Year 

Explanation

#include<stdio.h>
#include<conio.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 files The 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 year;
Ø int is a variable or data type.  A variable declared to be of type int can be used for storing integernumbers (values which doesn’t contain decimal places). Here we declare “year” int type. So this variable can take only integer values..
·       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”,&a”) is used here. So, this program will read in a integer value for “a” variable that the user enters on the keyboard (%d is for integer values. As there is “&a”, the number user enters will be the value of “a” variable).
·       if else loop
Ø If the expression evaluates to true, then the if block will be executed, otherwise, the else block will be executed. Here, we can see that in the brackets of if loop, there is a expression “year%4==0” .That means a will be divided by 4 and if the remainder is 0, then the code within if block will be executed. If the remainder is not 0, then the code within else block will be executed.
Ø   In the second if loop we check whether year is divisible by 100 and under that we check if it is divided by 400. If both of these conditions become true the year will be judged as a leap year. But if the second condition becomes false which means the year is not divisible by 400 then the year will not be a leap year. 
Ø  For both these if statements the else statement declares the year to be not a leap year.
·       getch() function
Ø getch() is used to hold the console(output) window on the screen after the whole program run is completed till the user enters a key from keyboard. This function is present in the header file called conio.h

Final Result

This program will show us whether a year is leap year or not. The year we enter, this program will divide the number by 4. If the remainder is 0, this program will say that the year is a leap year. If the remainder is not 0, this program will say that the year is not a leap year.

2 comments:

  1. This code is patently incorrect in terms of calculating leap years, but will tell you is a year is a multiple of 4.

    https://en.wikipedia.org/wiki/Leap_year#Algorithm

    ReplyDelete
    Replies
    1. Sir I am editing the code as soon as possible. Sorry for the fault and thanks for your reply.

      Delete

Powered by Blogger.