Tuesday, 12 June 2012

CHECKING LEAP YEAR


CHECKING LEAP YEAR USING C PROGRAM

Definition of leap year:

Rule 1: A year is called leap year if it is divisible by 400.
For example: 1600, 2000 etc leap year while 1500, 1700 are not leap year.
Rule 2: If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year.
For example:  2004, 2008, 1012 are leap year.

Leap year logic or Algorithm of leap year or Condition for leap year:

IF year MODULER 400 IS 0
 THEN leap_year
ELSE IF year MODULER 100 IS 0
 THEN not_leap_year
ELSE IF year MODULER 4 IS 0
 THEN leap_year
ELSE
 not_leap_year

Code 1:
1. C program to determine leap year
2. C program to find leap year or not
3. Leap year calculation in c

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

    printf("Enter any year: ");
    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);
  
    return 0;
}

Sample output:
Enter any year: 2010
2010 is not a leap year

Code 2:
1. Write a program to find leap year in c
2. How to find leap year in c code

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

    printf("Enter the lowest year: ");
    scanf("%d",&min_year);

    printf("Enter the heighest year: ");
    scanf("%d",&max_year);

    printf("Leap years in given range are: ");
    for(year = min_year;year <= max_year; year++){
         if(((year%4==0)&&(year%100!=0))||(year%400==0))
             printf("%d ",year);
    }
  
    return 0;
}

Sample output:
Enter the lowest year: 2000
Enter the highest year: 2011
Leap years in given range is: 2000 2004 2008

No comments:

Post a Comment