C Basic
C Interview Programs
- C Interview Programs
- C Abundant Number
- C Amicable Number
- C Armstrong Number
- C Average of N Numbers
- C Automorphic Number
- C Biggest of three numbers
- C Binary to Decimal
- C Common Divisors
- C Composite Number
- C Condense a Number
- C Cube Number
- C Decimal to Binary
- C Decimal to Octal
- C Disarium Number
- C Even Number
- C Evil Number
- C Factorial of a Number
- C Fibonacci Series
- C GCD
- C Happy Number
- C Harshad Number
- C LCM
- C Leap Year
- C Magic Number
- C Matrix Addition
- C Matrix Division
- C Matrix Multiplication
- C Matrix Subtraction
- C Matrix Transpose
- C Maximum Value of an Array
- C Minimum Value of an Array
- C Multiplication Table
- C Natural Number
- C Number Combination
- C Odd Number
- C Palindrome Number
- C Pascalβs Triangle
- C Power of 2
- C Power of 3
- C Pronic Number
- C Perfect Number
- C Perfect Square
- C Prime Factor
- C Prime Number
- C Smith Number
- C Strong Number
- C Sum of Array
- C Sum of Digits
- C Swap Two Numbers
- C Triangular Number
C Program to Check Leap Year
Photo Credit to CodeToFun
π Introduction
In the realm of programming, determining whether a given year is a leap year is a common requirement. A leap year is a year that is evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year. Checking for leap years involves implementing specific rules to validate the year.
In this tutorial, we will explore a C program designed to check whether a given year is a leap year.
π Example
Let's delve into the C code that accomplishes this task.
#include <stdio.h>
// Function to check if a year is a leap year
int isLeapYear(int year) {
// Leap year conditions
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
return 1; // True, it's a leap year
else
return 0; // False, it's not a leap year
}
// Driver program
int main() {
// Replace this value with the year you want to check
int year = 2024;
// Call the function to check if the year is a leap year
if (isLeapYear(year))
printf("%d is a leap year.\n", year);
else
printf("%d is not a leap year.\n", year);
return 0;
}
π» Testing the Program
To test the program with different years, modify the value of year in the main program.
2024 is a leap year.
π§ How the Program Works
- The program defines a function isLeapYear that takes an integer year as input and returns 1 (true) if the year is a leap year, and 0 (false) otherwise.
- The function checks leap year conditions: the year must be divisible by 4 and not divisible by 100, or it must be divisible by 400.
- Inside the main program, replace the value of year with the desired year you want to check.
- The program calls the isLeapYear function and prints the result.
π Between the Given Range
Let's dive into the C code that checks for leap years in the specified range.
#include <stdio.h>
// Function to check if a year is a leap year
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1; // Leap year
} else {
return 0; // Not a leap year
}
}
// Driver program
int main() {
// Define the range from 2000 to 2030
int startYear = 2024;
int endYear = 2050;
printf("Leap years in the range %d to %d:\n", startYear, endYear);
for (int year = startYear; year <= endYear; ++year) {
if (isLeapYear(year)) {
printf("%d ", year);
}
}
return 0;
}
π» Testing the Program
The provided code is already set to check for leap years in the range from 2024 to 2050.
Leap years in the range 2024 to 2050: 2024 2028 2032 2036 2040 2044 2048
Compile and run the program to see the leap years within this range.
π§ How the Program Works
- The program defines a function isLeapYear that checks if a given year is a leap year using the rules mentioned earlier.
- The main function initializes the range from 2000 to 2030 and iterates through each year.
- For each year, it calls the isLeapYear function and prints the year if it is a leap year.
π§ Understanding the Concept of Leap Year
Before delving into the code, let's understand the concept of leap years. Leap years are years that are evenly divisible by 4, except for end-of-century years, which must be divisible by 400 to be a leap year.
π’ Optimizing the Program
While the provided program is effective, consider exploring and implementing alternative approaches or optimizations for checking leap years.
Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!
π¨βπ» Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (C Program to Check Leap Yearο»Ώ) please comment here. I will help you immediately.