Leap year program in C

We will know about leap year program in C here. You may have know that leap year is the year which can be divisible by 400 and 4 but not divisible by 100. We can apply this logic to check that the given year is leap year or not.

We can make a leap year program using if else statement or switch statement. Here in this article we will make several leap year program in using C programming language.

Checking leap year using if else in C

// Leap year program in C
#include <stdio.h>
int main(){
  int year;

  printf("Enter the year you want to check : ");
  scanf("%d", &year);

  if (year % 400 == 0){ // checking if it is divisible by 400
    printf("\n%d is a leap year.\n", year);

  }else if (year % 100 == 0){ // checking if it is divisible by 100
    printf("\n%d is not a leap year.\n", year);

  }else if (year % 4 == 0){ // checking if it is divisible by 4
    printf("\n%d is a leap year.\n", year);

  }else{
    printf("\n%d is not a leap year.\n", year);
  }
  return 0;
}

Output of the leap year program:

leap year program in c

Check leap year using switch statement

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

  printf("Enter the year you want to check : ");
  scanf("%d", &year);

  int makeInt = (year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0);

  switch(makeInt){
  case 1:
    printf("Leap year.\n");
    break;

  default:
    printf("Not leap year.\n");
  }
  return 0;
}

See the output

Enter the year you want to check : 2020
Leap year.

Process returned 0 (0x0)   execution time : 5.803 s
Press any key to continue.

More C programming and projects

C learning topics