46 : C Program to Check Entered Number is a Leap Year or not

Check out the complete list of c-programs : C Program List    



What is a leap year ?

Earth takes 365.242199 days to complete its orbit around the sun. So we have leap years. A Leap year has 366 days (i.e febuary with 29 days as 1 day is added every 4th year) is called as a leap year. This happens every 4th year.

Note : To get to more closer to 365.242199 days/year, every 100th year is not a leap year!! Where as every 400th year is a leap year!

Logic to find leap year in C

Every 4th year is a leap year. And number divisible by 400 is a leap year, But number divisible by 100 is not a leap year! This can be achieved using if condition and modulo division with conditions like,


if ((year % 400 == 0) || ( ( year % 100 != 0) && (year % 4 == 0 ))) {
       
      //Leap year!
   }
   else {
       
       //Not a leap year!
   }



Variables :

year : Datatype int : Holds value of year inputted by the user. %d is the Format specifier for int's








/*
 * 1000+ C programs + tutorials
 *
 * 
 * 41_Check_entered_year_is_a_leapyear.c
 *
 *
 *  Created on: Oct 20, 2014
 *  Author: Code2care.org
 */

#include 
//#include 



void main() {

int year;
  

//clrscr();

    printf("C Program to Find Entered Year is a leap year or not.");

    printf("\n\n Enter Number : ");
    scanf("%i", &year);

    

     
     
  //Logic to check leap year

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

//getch();


}









The best way to learn C programming is to practice more and more of programs . Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.

BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.