How to Find Leap Year Using C Programming?
To determine whether a year is a leap year in C, follow these steps:
- A year divisible by 400 is a leap year.
- A year divisible by 100 but not by 400 is not a leap year.
- A year divisible by 4 but not by 100 is a leap year.
- A year not divisible by 4 is not a leap year.
You can implement these checks in a C program to evaluate whether a given year is a leap year or not.
Pseudocode for a Leap Year C Program
- Start
- Input: Read the year from the user.
- Check Conditions:
- If the year is divisible by 400, it is a leap year.
- Else if the year is divisible by 100, it is not a leap year.
- Else if the year is divisible by 4, it is a leap year.
- Otherwise, it is not a leap year.
- Output: Print whether the year is a leap year or not.
- End
Program to check Leap Year or not using If Statements.
In the below program, we asked the user for input year and checked using modulus operator if the given year is divisible by 400 or given Year divisible by four but not by 100 if any one of the conditions becomes true, we enter if and print user Entered Year is a leap year.
Implementation
C
// Include header files
#include <stdio.h>
int main()
{
// Declare Variables
int Year;
// Ask User to input Year
printf("\n Enter Year you want to check:- \n ");
// Store user provided input in variable Year
scanf("%d", &Year);
// If statement checks if year is divisible by 400 if not Is Year divisible by 4 but not by 100 . If any one of the condition becomes true we print its Leap year else not a Leap Year
if (( Year%400 == 0) || (( Year%4 == 0 ) && ( Year%100 != 0))){
printf("\n Given Year is a Leap Year. \n");
}
else{
printf("\n Given Year is not a Leap Year. \n");
}
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output 01:-
Enter Year you want to check:-
2000
Given Year is a Leap Year.
Output 02:-
Enter Year you want to check:-
1999
Given Year is not a Leap Year.
You can also read about dynamic array in c and Short int in C Programming
Program to Check Leap Year using If Else Statements.
In the below program, we have used the same condition to distinguish between a leap year or not, as used in the above program. We have just split the if condition into multiple parts. We ask the user for input. And check if user-provided input is divisible by 400 or not. If the condition becomes true, it prints a Leap year and returns from the main. Otherwise, it goes for the following condition defined in else if statements, i.e. divisibility by 100 and divisibility by 4. If all conditions are false, then we enter else block and execute. At the same time, while using the else if statement, one should keep in mind the order of condition put in else if.
Implementation
C
// Include header files
#include <stdio.h>
// Define main function
int main() {
// Declare int variable
int Year;
printf("Enter Year : \n");
scanf("%d", &Year);
// Based on properties of Leap year we define if a year divisible by 400 is a Leap year.
if (Year % 400 == 0) {
printf("Given Year is a Leap Year.");
}
// Since it escaped the if statement, it means Year is not divisible by 400. and we any year not divisible by 400 but divisible by 100 is not a Leap year
else if (Year % 100 == 0) {
printf("Given Year is not a Leap Year.");
}
// Since it escaped the above two conditions, it's not divisible by 400 and 100. So we check the last one condition for a year to be a leap year, i.e., divisibility by 4
else if (Year % 4 == 0) {
printf("Given Year is a Leap Year.");
}
// Rest all years are not a leap year
else {
printf("Given Year is not a Leap Year.");
}
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output 01:-
Enter Year :
2000
Given Year is a Leap Year.
Output 02:-
Enter Year :
1999
Given Year is not a Leap Year.
Program to check Leap Year or not using Nested If
In the below program, we have used the same condition to distinguish between a leap year or not, as used in the above programs. We have used the if condition inside if statements, i.e., Nested if. We ask the user for input. And check if user-provided input is divisible by four or not. If the condition becomes true, it again checks if it's divisible by 100. If yes, it again uses the if condition to check if a year is divisible by 400. If the statement becomes true, it prints Leap year; otherwise, it executes the respective else block. If the first conditions are false, we directly enter the else block and execute Not a Leap year.
Implementation
C
// Include header files
#include <stdio.h>
// Define main function
int main()
{
// Declare int variable
int Year;
// Asking user for the input
printf("\n Enter Year :- \n");
// Storing user-input in Variable Year
scanf("%d",&Year);
// Nested if conditions
// First condition checked if Year is divisible by 4 or not
if(Year%4 == 0) {
// If year divisible by 4 we check if it's divisible by 100
if( Year%100 == 0) {
// Since the year is divisible by 100 its a century year we check for divisibility by 400
if ( Year%400 == 0) {
printf("\nGiven Year is a Leap Year.");
} else {
printf("\nGiven Year is not a Leap Year.");
}
//Year divisible by four but not by 100. So it's not a century.
}
else {
printf("\nGiven Year is a Leap Year" );
}
}
//Year is not divisible by 4. The primary condition for any year to be a leap year
else {
printf("\nGiven Year is not a Leap Year.");
}
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output 01:-
Enter Year:-
2000
Given Year is a Leap Year.
Output 02:-
Enter Year:-
1999
Given Year is not a Leap Year.
C Program to Find Leap Years Within a Given Range
To find all the leap years within a given range, you can write a C program that iterates through each year in the range and applies the leap year conditions to determine if a year is a leap year. The program will then print all the leap years that fall within the specified range.
Example:
C
#include <stdio.h>
int main() {
int startYear, endYear;
// Input start and end year
printf("Enter the start year: ");
scanf("%d", &startYear);
printf("Enter the end year: ");
scanf("%d", &endYear);
// Print leap years within the range
printf("Leap years between %d and %d are:\n", startYear, endYear);
for (int year = startYear; year <= endYear; year++) {
if ((year % 400 == 0) || (year % 100 != 0 && year % 4 == 0)) {
printf("%d\n", year);
}
}
return 0;
}
You can also try this code with Online C Compiler
Run Code
Output:
Enter the start year: 2000
Enter the end year: 2020
Leap years between 2000 and 2020 are:
2000
2004
2008
2012
2016
Frequently Asked Questions
How to check if a year is a leap year or not in C language?
Property of Leap year like divisibility by four or divisibility by 400 if it's divisible by 100 are used to check if a year is a leap year or not. Based on the above properties, we can use if-else statements, Nested if statements, Else if statements to evaluate those conditions.
What is the vital thing to keep in mind when using Else if statements?
While using the else if statement to evaluate the condition, one should keep in mind the order of condition put in else if statements. Order plays a vital role in Else if statements.
What are Nested if statements in the C programming language?
Nested if statements are used to evaluate a condition inside another condition.
What is the formula for leap year?
A year is a leap year if:
- It is divisible by 400, or
- It is divisible by 4 but not by 100.
Conclusion
In the article, we have extensively discussed the C Program to Check Leap Year or Not. Determining whether a year is a leap year is a fundamental task that highlights key concepts in programming, such as conditional statements and modular arithmetic. The C program provided demonstrates a straightforward approach to checking leap years by evaluating specific conditions. By understanding and implementing these checks, you can effectively handle date-related calculations and improve your programming skills.
We hope this blog has helped you enhance your knowledge regarding the C programming language. To learn more about such content, practice some quality questions that require you to excel at your preparations. Visit our Guided Path in Coding Ninjas Studio.To be more confident in data structures and algorithms, try out our DS and Algo Course. Till then, All the best for your future endeavours, and Keep Coding.