Yesterday, I have explained about creating Leap year program in Java, today I will create the same program to check if a year is leap year or not year in C. The good thing about C & Java they are almost same languages in terms of writing code, as in all programming languages only Syntax changes, code algorithm & basic concepts like loop, conditions etc remains same, so if you know C you can learn Java easily, vice-versa is also true.

Now, let's get started from basics again, before we proceed you must know what is leap year? and basic algorithm to create leap year program in any language.

What is leap year?

Usually there are 365 days in a year. However once every four years, there are 366 days in year. This year is called leap year and on a leap year, February has 29 days. However not all years divisible by 4 are leap years.

There are two criteria's for a year to be leap year:

  • A leap year is evenly divisible by 4 except the century years (years like 1200, 1300, 2400, 2500 etc)
  • For century years:
    • If the century year is divisible by 400 then that year is a Leap year
    • If the century year is not divisible by 400 then that year is not a Leap year

Basic Algorithm

if 
 (year is not divisible by 4) then (it is a common year)
else if 
  (year is not divisible by 100) then (it is a leap year)
else if 
  (year is not divisible by 400) then (it is a common year)
else 
  (it is a leap year)

Leap year program in C using if-else ladder

Considering the above algorithm, let's create leap year program using if-else

#include<stdio.h>
void main()
{
  int year;
  printf("Enter year of your choice: ");
  scanf("%d", &year);
  
  if ( year%400 == 0) //Divisible by 400
  {
     printf("%d is a leap year.\n", year);
  }
  else if (year%100 == 0) //Divisible by 100 and not by 400
  {
     printf("%d is not a leap year.\n", year);
  }
  else if ( year%4 == 0 ) //divisible by 4 and neither by 100 nor 400, leap year found
  {
     printf("%d is a leap year.\n",year);
  }
  else //no condition is true, so not a leap year
  {
     printf("%d is not a leap year.\n", year);  
  }
    
}

Executing the above program provide me output as 

Enter year of your choice: 2017 
2017 is not a leap year.

leap-year-program-in-c-min.png

Leap year program in C using simple if-else statement

#include<stdio.h>
void main() 
 {
    int year;
    printf("Enter any year of your choice : ");
    scanf("%d",&year);
    if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))
    {
      printf("%d is a leap year", year);
    }
    else
    {  
        printf("%d is not a leap year", year);
    }
    
 }

Output:

Enter any year of your choice : 2016                                                                                  
2016 is a leap year

leap-year-program-in-c-using-single-if-else-min.png

Leap year program in C using nested if-else

#include<stdio.h>
void main()
{
    int year;
    printf("Enter any year of your choice: ");
    scanf("%d" , &year);  //get year
 
    if(year % 4 == 0) //if TRUE may be a leap year
    {
        if( year % 100 == 0) //check second condition, if false not a leap year
        {
            if ( year%400 == 0)      //check thrid condition, if year is divisible by 400, it is leap year
            {
                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);
    }
}

I tried executing the above program on online compiler here is the output.

Enter any year of your choice: 2019
2019 is not a leap year.

leap-year-program-in-c-using-nested-if-else-min.png

Few more articles, related to C language which you may like


That's it, we are done, feel free to ask any question related to this post in comment's section or in question's section.