Responsive Ads Here

Tuesday, November 25, 2014

Leap year - logic and Math

 

The algorithm or logic for Leap year is basically that:
1. A year is a leap year if it is divisible by 4 but not by 100.
2. If a year is divisible by both 4 and by 100, then it can only be a leap year if it is also divisible by 400.

 

#include <stdio.h>
int main()
{
    int year;
    printf("Enter a year: ");
    while(scanf("%d",&year)!=EOF)
    {
 
        if(year%4 == 0)
        {
            if( year%100 == 0) /* Checking for a century year */
            {
                if ( year%400 == 0)
                    printf("This is a leap year.\n");
                else
                    printf("This is an ordinary year.\n");
            }
            else
                printf("This is a leap year.\n");
        }
        else
        {
            printf("This is an ordinary year.\n", year);
        }
                printf("\nEnter a year: ");
 
    }
    return 0;
}

No comments:

Post a Comment

php4

<?php    // Start the session  session_start();  ?>  <!DOCTYPE html>  <html>  <head>       <link rel=&q...