C Program for checking a Leap year entered by user

No comments
In this tutorial we will write a program which will ask the user to enter a year through keyboard and then print the result about that year whether it is a leap year or not.Before starting to code ,we must know in detail what a leap year is and what are the criteria a year must satisfy to be a leap year. It will help us in applying our logic and writing a perfect program without any error.
What is a leap year A leap is year is a year which has 366 days instead of 365 which a common year has. We have a February of 29 days in a leap year. A leap year occurring once every four Years.Now it's clear that a leap year is divisible by 4, but wait there is more to know about a leap year.
Criteria a year must satisfy for being a leap year The C program which we are going to write for checking a leap year is based on the rules defined by the Gregorian calendar for a leap year.
According to Georgian calendar these criteria must be satisfied for a year to be a leap year –
  • The year must be divisible by 4.

  • A year is not a leap year if it is evenly divisible by 100 unless it is also divisible by 400.
    For example-2400 and 2000 are lep years but 1800,2100,2300 etc are not leap years.
Now we will use above criteria for writing our C program for checking a leap year.
Note-Take help of the comments for a better understanding.
Program-
#include <stdio.h>

void main()
{
  int year; //year to be entered by user
 
//asking user to enter a year ,which he/she want to check  
printf("Enter the year to check\n");

//scan the entered value and put it into year variable.  
scanf("%d", &year);
 
//checking for a leap year according to the criteria described and printing the results.  

if (year%400 == 0)
    printf(" year %d is a leap year.\n", year);
  
else if (year%100 == 0)
    printf(" year %d is not a leap year.\n", year);
  
else if (year%4 == 0 )
    printf(" year %d is a leap year.\n", year);
  
else
    printf("year %d is not a leap year.\n", year);  
 
}


No comments :

Post a Comment