Adsense 728 * 15

Search

Custom Search

Program to Check Leap Year in C, C++ - Program to Check whether a given Year is Leap Year or Not

Write a C, C++ program to check whether a entered year is a leap year or not. In this program, A user will input a year and our program will check whether it's a leap year or not.

What is Leap Year

A Leap year is a year which has 366 days, it means it has one additional day. Each leap year has 366 days instead of the usual 365 days.

Leap Year Program in C, C++


How to Check Leap Year


i) If a year is divisible by 4 then it's a leap year.  For example - 2012, 2008 etc.

ii) If a century year(1200, 1600) is divisible by 400  then it's a leap year. For example - 1200, 1600 etc.

C, C++ Interview Questions

Leap Year Program in C


#include <stdio.h>

int main(void) {
 
      int year;
   
      printf("Enter a year\n");
      scanf("%d",&year);
      
      /* A year should be divisible by 4 or 400 
         but not divisible by 100 */
      
      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);
      }

     return 0;     
}


Another way of writing above program is -

#include <stdio.h>

int main(void) {
 
      int year;
   
      printf("Enter a year :");
      scanf("%d",&year);
      
      /* Check whether a entered year 
         is divisible by 4 */
      
      if(year%4 == 0)
      {
         /* For century year such as 500,1200 etc */
         
          if( year%100 == 0) 
          {
             /* If it's divisible by 400 then it's a leap year */
             
              if ( year%400 == 0)
              {
                 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);
      }
      
      return 0;
      
}



Output:


Enter a year  :  2008

2008 is a leap year

No comments:

Post a Comment