In the Mini project, next module is to check whether a given year is the leap year or not. Rita allotted this function to Johnny. Rita gave a hint about leap year conditions to Johnny.

Hint:
In general terms, the algorithm for calculating a leap year is as follows...
A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.
Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.
Help Johnny to write a program to check whether a given year is a leap year or not?

Function Specification:
int leapyear(int year);
The function returns 1 if it is the leap year, else it returns 0.

Input Format:
Input consists of a single integer.
Output Format:
The output consists of a single line.
Refer sample input and output for formatting details.

Sample Input 1:
1988
Sample Output 1:
1988 is a leap year

Sample Input 2:
1994
Sample Output 2:
1994 is not a leap year

Program Code:
#include <stdio.h>
int leapyear (int year) 
{
if(year%4 == 0)
{
if( year%100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year%400 == 0)
return 1;
else
return 0;
}
else
return 1;
}
else
return 0;
}
int main()
{
int year,k;
scanf("%d",&year);
k=leapyear(year);
if(k==1)
printf("%d is a leap year",year);
else
printf("%d is not a leap year",year);
return 0;
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: