Write a C Program to Check Leap Year.
A leap year, in the Gregorian calendar, is a year that is evenly divisible by
4, except for century years (years ending with 00), which must be divisible by
400 to be considered a leap year. The concept of a leap year is important for
ensuring that our calendar remains synchronized with the Earth's revolutions
around the Sun. In this article, we will guide you through the process of
writing a C program to check whether a given year is a leap year or not,
without using structures and functions.
The Logic Behind Leap Years
Before delving into the C program itself, let's understand the logic behind
determining leap years:
- If a year is divisible by 4 and not divisible by 100, it is a leap year.
- If a year is divisible by 100 and also divisible by 400, it is a leap year.
Here's a C Program to Check Leap Year:
#include<stdio.h>
#include<conio.h>
void main()
{
int year; // Taking input from the user
printf("Enter a year: ");
scanf("%d", &year);
// Checking if it's a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
}
#include<conio.h>
void main()
{
int year; // Taking input from the user
printf("Enter a year: ");
scanf("%d", &year);
// Checking if it's a leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
printf("%d is a leap year.\n", year);
}
else
{
printf("%d is not a leap year.\n", year);
}
}
- We declare an integer variable year to store the input year.
- The user is prompted to enter a year, and their input is stored in the year variable.
- The program then checks the conditions for a leap year:
- (year % 4 == 0 && year % 100 != 0) checks if the year is divisible by 4 but not divisible by 100.
- (year % 400 == 0) checks if the year is divisible by 400.
- If either of the conditions is satisfied, the program prints that the input year is a leap year; otherwise, it prints that the year is not a leap year
Output: