Wednesday 7 August 2013

Find Leap Year Program

I'm now completely into the database program (8k+ lines of code now), that's why I don't have much to post these days, because I'm not writing any short programs.

The feature I'm trying to put in right now is that the user will enter 2 dates and the program does something with them (displays information based of the sales between those dates).

The program needs to know how many days are in between, so if the user entered dates spans more than 365 days, I have to figure out leap years so that the number of days are accurate.

So I sat down to write a program I could use as a function. The program would accept a year and return if it is a leap year or not.

I remembered googling something to the effect of  "how to tell if a year is a leap year" once, & saved the answer I found in a notepad file. But I forgot the formula, and I was not up for finding that notepad file, so I googled it again.

HERE is what I found. Based on the best answer to the question there, I wrote a program. Here it is:

#include <stdio.h>
#include <stdlib.h>

main()
{
    int year,leap=0;

    system("cls");
    printf("Enter year, 0 to exit: ");
    while(1)
    {
        leap=0;
        scanf("%i",&year);
        if(year==0)
        {
            break;
        }
        if(year%4==0)
        {
            if(year%100==0)
            {
                if(year%400==0)
                {
                    printf("Year is a leap year!\n");
                    leap=1;
                }
                else
                {
                    leap=0;
                }
            }
            else
            {
                printf("Year is a leap year!\n");
                leap=1;
            }
        }
        if(leap==0)
        {
            printf("Year is NOT a leap year!\n");
        }
        printf("Enter year, 0 to exit: ");
    }
    return(0);
}

But when I was trying to save it as "LeapYear.c", the autocomplete showed me another file called "LeapYear.cpp". So I checked it out. Here it is:

#include <stdio.h>

main()
{
    int year;

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

    if((year%4==0 && year%100!=0) || (year%400==0))
    {
        printf("%i is a leap year.",year);
    }
    else
    {
        printf("%i is not a leap year.",year);
    }
}

The second one is much shorter.
Since I decided to post it here, I found the Notepad file, here is the explanation on finding out if a particular year is a leap year.

Check if the year is divisible by 4.
If not, it is not a leap year.

If it is divisible by 4, then:
Check if the year is divisible by 400, if it is, it is a leap year.
Check if the year is divisible by 100, if it is, it is not a leap year.

Otherwise, it is a leap year.

I found that on a page in Yahoo Answers, but I cannot find that page right now.

Anyway, I may try to include a calendar displayed in a grid so that it is easier for the user to choose dates. If I do, I will write a smaller program based on that and post it next.

No comments:

Post a Comment