C language program code for Leap Year

C & C++ teachers in a lot of schools use leap year calculation program as a classic example to demonstrate the nested if-else conditional scenarios of these languages.

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.

  • Going by the above logic, any year that is divisible by 400 must definitely be a leap year (because any number divisible by 400, is also divisible by 4)
  • If a number is not divisible by 400, then we can check whether it is divisible by 100. If that is the case then it is definitely not a leap year (even if it is divisible by 4. this is as per condition no.1 above)
  • After checking divisibility of the year by 400, and 100, lastly we can check whether the number is exactly divisible by 4. If it is, then its a leap year, else its not a leap year.
  
A simple C / C++ programming code that uses this logic for determining whether a year is a Leap year or not is given below:

#include<stdio.h>
#include<conio.h>
void main()
{
 int year;
 clrscr();
 printf("Enter the Year that you want to check : ");
 scanf("%d", &year);
  
 if(year % 400 == 0)
     printf("%d is a Leap Year.", year);
else
     if(year % 100 == 0)
         printf("%d is not a Leap Year.", year); 
    else
         if(year % 4 == 0)
              printf("%d is a Leap Year.", year);
         else
              printf("%d is not a Leap Year", year); 
              printf("\nPress any key to Quit...");
              getch();
}

27 comments:

  1. mine's a little different.
    (consider "x" as a variable for year.)

    #include
    #include
    main()
    {
    int x;
    printf("Enter a year: ");
    scanf("%d",&x);
    for(x=x;x>0;x=x-4)
    {
    }
    if(x==0)
    {
    printf("\nIt's a leap year!");
    }
    else
    {
    printf("\nIt's not a leap year!");
    }
    getch();
    }

    ReplyDelete
    Replies
    1. The complexity is high here and will increase as the x increases.
      complexity is O(X/4).... whereas the above the complexity is O(1).

      Delete
  2. simlest program to cheak a leap year :)



    #include
    #include
    main()
    {
    int year;
    printf("enter a year");
    scanf("%d",&year);
    if (year%100==0)
    {
    if (year%400==0)
    printf("it is a leap year");
    else
    printf("it is a not leap year");
    }
    else
    {
    if (year%4==0)
    printf("it is a leap year");
    else
    printf("it is a not leap year");
    }
    getch();
    }

    ReplyDelete
  3. i guess i've got a shortter and better solution for this...so it is something like this
    #include
    int main()
    {
    int year;
    printf("enter the year");
    scanf("%d",&year);
    if(year%400==0||year54==0&&year%100!=0)
    printf("it is a leap year");
    else
    printf("it is not a leap year");
    return 0;
    }

    ReplyDelete
  4. int main()
    {
    int year;
    printf("enter the year");
    scanf("%d",&year);
    if(year%4==0)
    printf("it is a leap year");
    else
    printf("it is not a leap year");
    return 0;
    }
    ...............................................................
    is that code right ???
    please explain ....

    ReplyDelete
    Replies
    1. yeah.
      your answer is right.
      Because, if you divide by 4 and there is no remaining then that is leap year. hope its simple to understand the logic. :)

      Delete
    2. A leap year is every 4 years, but not every 100 years, then again every 400 years.
      If logic is %4==0
      Then it failed in the case of 1900 which is not a leap year but which is divided by 4

      Delete
  5. i guess the simplest one is........
    #include
    #include
    void main()
    {
    int a;
    clrscr();
    printf("Enter the year:");
    scanf("%d"&a);
    if(a%4==0)
    {
    if((a%100==0 && a%100!=0))
    printf("The %d year is NOT a LEAP year");
    else
    printf("The %d year is a LEAP year");
    }
    else
    {
    printf("The %d year is NOT a LEAP year");
    }
    getch();
    }

    ReplyDelete
    Replies
    1. There will be an error in " scanf("%d"&a); " this line and other others lines also.

      improve yourself :)

      Delete
  6. 1900 is not a leap year so 1904 also not a leap year both are div by 4
    2000 and 2004 is a leap year any of these programs not satisfying these conditions

    ReplyDelete
    Replies
    1. 1904 is a leap year
      A leap year is something which comes for every years
      But not divisible by 100
      But divisible by 400

      Delete
  7. This comment has been removed by the author.

    ReplyDelete
  8. #include
    int main()
    {
    int n;
    printf("enter the year:-");
    scanf("%d",&n);
    if(n>0)
    {
    printf("valid input\n");
    if(n%4==0&&n%100!=0||n%400==0)
    {
    printf("this is leap year\n");
    }
    else
    {
    printf("this is not leap yaer\n");
    }
    }
    else
    {
    printf("invalid input\n");
    }
    }

    ReplyDelete
  9. "C program to check leap year"
    Question:
    How do i tweak the output of this code:
    ~let say i type in 2012 and the output should be February 29, 2014 Wednesday is a leap year (same thing with the other leap year)
    ~ i type in 2013 and the output should be 2013 is not a leap year.
    Thanks in advance.

    ReplyDelete
  10. check for the year 2700,2500 these are exceptional cases,and also check calendar of these years pls thank you

    ReplyDelete
  11. why to use 100 to check leap year in c?

    ReplyDelete
    Replies
    1. The leap year will not repeat for 100 years but it will repeat for every 400 years

      Delete
  12. //----- This is perhaps simplest of all:
    int set_max_days_for_Feb(int year)
    {if (year & 3 )return 28; // year within century
    if (year % 100 )return 29; // leap centuries: 1800,1900,2100
    if (year % 400 )return 28; // leap centuries (1600,2000,2400,...)
    return 29;
    }

    ReplyDelete
  13. This comment has been removed by the author.

    ReplyDelete
  14. #include

    int main ()
    {
    int year;

    scanf ("%d", &year);

    if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0) {
    printf ("The given year is a leap year\n");
    }
    else {
    printf ("The given year is not a leap year\n");
    }

    return 0;
    }

    ReplyDelete
  15. This comment has been removed by the author.

    ReplyDelete
  16. #include
    using namespace std;

    int main()
    {
    int year,div;
    cout<<"input an year:";
    cin>>year;
    if(year%100==0)
    div=year%400;
    else
    div=year%4;
    if(div==0)
    cout<<"Leap Year..";
    else
    cout<<"not a leap year";
    return 0;
    }

    ReplyDelete