Leap year or not
C program to determine whether the year entered by user is a leap year or not.
An year is said to be leap year if
For example,
year = 1600,
year = 1700,
The above problem can be programmed in two ways:
year is a multiple of 4 (except for years evenly divisible by 100, which are not leap years unless evenly divisible by 400). |
For example,
year = 1600,
1600 is divisible by both 100 and 400. So, 1600 is a leap year. |
year = 1700,
1700 is divisible by 100, but not divisible by 400. So, 1700 is not a leap year. |
The above problem can be programmed in two ways:
Using if-else construct, which is explained in method 1 tab. |
Using nested if construct, which is explained in method 2 tab. |
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf("\nEnter the year\t: ");
scanf("%d",&year);
if((year%4==0) && (year%100!=0))
{
printf("\nLeap year");
}
else if((year%4==0) && (year%100==0) && (year%400==0))
{
printf("\nLeap year");
}
else
{
printf("\nNot a leap year");
}
getch();
}
Output
********** Run1 **********
Enter the year : 1600
Leap year
********** Run2 **********
Enter the year : 1700
Not a leap year
Explanation
A year can be checked whether it is leap year or not by dividing the year by 4, 100 and 400.
If a number is divisible by 4 but not by 100 then, it is a leap year.
Also, if a number is divisible by 4, 100 and 400 then it is a leap year.
Otherwise, the year is not a leap year.
The above conditions are checked using if-else construct
Code to check first condition "If a number is divisible by 4 but not by 100" is
For second condition "if a number is divisible by 4, 100 and 400" the code is,
If a number is divisible by 4 but not by 100 then, it is a leap year.
Also, if a number is divisible by 4, 100 and 400 then it is a leap year.
Otherwise, the year is not a leap year.
The above conditions are checked using if-else construct
Code to check first condition "If a number is divisible by 4 but not by 100" is
((year%4==0) && (year%100!=0))
For second condition "if a number is divisible by 4, 100 and 400" the code is,
((year%4==0) && (year%100==0) && (year%400==0))
Program
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
printf("\nEnter the year\t: ");
scanf("%d",&year);
if(year%4 == 0)
{
if(year%100 == 0)
{
if(year%400 == 0)
{
printf("\nLeap year");
}
else
{
printf("\nNot a leap year");
}
}
else
{
printf("\nLeap year");
}
}
else
{
printf("\nNot a leap year");
}
getch();
}
Output
********** Run1 **********
Enter the year : 1600
Leap year
********** Run2 **********
Enter the year : 1700
Not a leap year
Explanation
A year can be checked whether a year is leap year or not by dividing the year by 4, 100 and 400.
If a number is divisible by 4 but not by 100 then, it is a leap year.
Also, if a number is divisible by 4, 100 and 400 then it is a leap year.
Otherwise, the year is not a leap year.
The above conditions are checked using Nested if construct
Here, the year entered by the user is firstly divided by 4. If it is divisible by 4 then it is divided by 100 and then 400.
If year is divisible by all 3 numbers then that year is a leap year.
If the year is divisible by 4 and 100 but not by 400 then it is not a leap year.
If the year is divisible by 4 but not by 100, then it is a leap year. (Remember that if the year is divisible by 4 and not by hundred then the program does not check the last condition, i.e., whether the year is divisible by 400 or not).
If the year is not divisible by 4 then no other conditions are checked and the year is not a leap year.
If a number is divisible by 4 but not by 100 then, it is a leap year.
Also, if a number is divisible by 4, 100 and 400 then it is a leap year.
Otherwise, the year is not a leap year.
The above conditions are checked using Nested if construct
Here, the year entered by the user is firstly divided by 4. If it is divisible by 4 then it is divided by 100 and then 400.
If year is divisible by all 3 numbers then that year is a leap year.
If the year is divisible by 4 and 100 but not by 400 then it is not a leap year.
If the year is divisible by 4 but not by 100, then it is a leap year. (Remember that if the year is divisible by 4 and not by hundred then the program does not check the last condition, i.e., whether the year is divisible by 400 or not).
If the year is not divisible by 4 then no other conditions are checked and the year is not a leap year.