ADVERTISEMENT
Given Year Is Leap Year Or Not
This C program checks whether a given year is a leap year or not. A leap year is a year that is evenly divisible by 4, except for years that are divisible by 100 but not by 400.
#include<stdio.h>
main()
{
int year;
printf("Enter the year you want to check\n");
scanf("%d",&year);
if(year%400==0)
{
printf("%d is leap year\n",year);
}
else if(year%100==0)
{
printf("%d is not a leap year\n",year);
}
else if(year%4==0)
{
printf("%d is a leap year\n",year);
}
else
{
printf("%d is not a leap year\n",year);
}
}C Programming ExamplesExplanation:
Here’s a step-by-step explanation of the code:
Header File:
- The program includes the standard C library header <stdio.h>. This header is required for input and output operations.
Main Function:
- The main function is the entry point of the program, where the execution begins.
- Variable Declaration:
- The program declares an integer variable ‘year’ to store the user’s input (the year to be checked).
User Input:
- The program prompts the user to enter a year with the printf function and reads the input using scanf. The entered year is stored in the ‘year’ variable.
Leap Year Checking:
- The program uses a series of if-else if-else statements to check whether the entered year is a leap year or not.
The conditions for leap years are:
- If the year is divisible by 400 (year % 400 == 0), it’s a leap year.
- If the year is divisible by 100 but not by 400 (year % 100 == 0), it’s not a leap year.
- If the year is divisible by 4 (year % 4 == 0), it’s a leap year.
- If none of the conditions is met, the year is not a leap year.
Output:
- The program uses printf to display the result of the leap year check.
- If the year is a leap year, it prints that the year is a leap year.
- If the year is not a leap year, it prints that the year is not a leap year.
End of Program:
- Once the result is printed, the program reaches the end of the main function, concluding the program’s execution.
This code takes a user-entered year as input and checks whether it’s a leap year based on the conditions mentioned earlier. It then displays the result to the user.