Leap Year: The year that contain 366 days instead of 365 days is known as a leap year.With February, this year, has 29 days.
Simply put, a leap year is a year with an extra day—February 29—which is added nearly every four years to the calendar year
A leap year is exactly occurs every 4 years.The leap Year is divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.
e.g 2020 is Leap Year
2022 is Not Lear Year
2000 is Leap Year
2002 is not Leap Year
#include <stdio.h>
/* Write C Program to find all leap years from 1 to N using while loop provided by the user.*/
#include <stdio.h>
int main()
{
int year;
int min_year,max_year;
printf("\nEnter the minimum year: ");
scanf("%d",&min_year);
printf("\nEnter the maximum year: ");
scanf("%d",&max_year);
printf("\nLeap years in given range are:\n ");
year=min_year;
while(year<=max_year)
{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
{
printf(" %d",year);
}
year++;
}
return 0;
}
Output:
Enter the minimum year:
2000
Enter the maximum year:
2024
Leap years in given range are:
2000 2004 2008 2012 2016 2020 2024
Program Explanation:
1. In the program given above the variables year, min_year,max_year is declared as integer variable.
2. printf("\nEnter the minimum year: ");
scanf("%d",&min_year);
These statements displays message "Enter the minimum Year:" and control wait at the memory location &min_year until user do not enter the value for min_year.Once you enter the value for a year ( the value for min_year enterd by us is 2000) scanf("%d",&min_year) store the entered value(integer value .i.e 2000) in the variable min_year, now the value for min_year=2000
similarly
printf("\nEnter the maximum year: ");
scanf("%d",&max_year);
displays message "Enter the maximum year:" and control wait at the memory location &max_year until user do not enter the value for max_year.Once you enter the value for a max_year (The value for max_year enterd by us is 2024 ) ,scanf("%d",&max_year) store the entered value (integer value i.e. 2024) in the variable max_year. now max_year=2024.
Note: "min_year and max_year are the variable they are used to store the year values. The min_year and max_year are the range of years in which we want to find Leap Years."
3. printf("\nLeap years in given range are:\n ");
year=min_year;
The printf() displays the message "Leap years in the given range are:"
and the value in min_year is assigned to the variable year.
The value for year is same as the value in min_year.
The variable year becomes
year=2000.
The variable year is a variable whose value will be incremented by one from min_year to max_year and tested with max_year.
after executing these statements control jumps to while loop().
4. while(year<=max_year)
{
if(((year%4==0)&&(year%100!=0))||(year%400==0))
{
printf(" %d",year);
}
year++;
}
4.1 The Condition while(year<=max_year) tested in the while loop is true(while (2000>=2024)) then control jumps inside body of the loop and start executing the statement/condition if(((year%4==0)&&(year%100!=0))||(year%400==0))
i.e. if(((2000%4==0)&&(2000%100!=0))||(2000%400==0))
2000%4==0 which is true and 2000%100!=0 is also true .
The expression year%400==0 is also true, combining all the condition in if statement evaluates true then body of the if executes the statement printf(" %d",year); get processed and displays integer(%d) value of the year on console i.e. 2000.
The Statement year++ is executed whose value is incremented by 1, now year becomes 2001.
The control goes back to test the while(year<=max_year) condition.
4.2 if the condition while(year<=max_year) tested is true then body of loop executes as explained in previous step.
This process continues while value of year is less than or equal to(<=) to the max_year.
5. Finally control comes out of loop and stops the execution.