C PROGRAM
Check for leap year |C Program|
Introduction
Checking for leap years is a common task, especially in date-related calculations. This C program showcases a straightforward approach to determine whether a given year is a leap year or not. The logic employed here considers the rules for leap years, making it an educational example for understanding basic conditional statements in programming.
Code
#include<stdio.h>
#include<conio.h>
void main() {
int year;
clrscr();
// Input
printf("Enter the year: ");
scanf("%d", &year);
// Leap Year Logic
if (year % 4 == 0) {
if (year % 100 == 0) {
if (year % 400 == 0) {
printf("%d is a leap year.", year);
} else {
printf("%d is not a leap year.", year);
}
} else {
printf("%d is a leap year.", year);
}
} else {
printf("%d is not a leap year.", year);
}
getch();
}
Output
Enter the year: 2020
2020 is a leap year.
In this example, the user inputs the year 2020. The program then checks the conditions for a leap year (year % 4 == 0, year % 100 == 0, year % 400 == 0) and correctly identifies that 2020 is a leap year.
Explanation
1. Header Files:
The program includes the standard input/output header <stdio.h> and the console input/output header <conio.h>.
2. Main Function:
The main function serves as the entry point of the program.
3. Input:
Users input the year to be checked.
4. Leap Year Logic:
The program employs nested if statements to determine if the year is a leap year.
It checks whether the year is divisible by 4, 100, and 400, adhering to the rules for leap years.
5..Output:
The program outputs whether the given year is a leap year or not.
Conclusion
This C program provides a practical example of determining leap years using conditional statements. Understanding leap year logic is essential for various applications, including calendar systems and date calculations.
Feel free to run the program with different years to observe how it correctly identifies leap years based on the specified conditions. This example serves as a valuable introduction to leap year determination in C. Happy coding!
Post a Comment
0 Comments