Checking for leap years is crucial in applications like calendar management, date validation, and scheduling, where accurate date calculations are essential. In this tutorial, we’ll write a C program to find leap year and determine if a given year meets the conditions.
This program
to check leap year in C introduces logical operators and conditional
statements, which are foundational in programming. By understanding leap year
rules and implementing them in code, you’ll be better prepared to handle
real-world date-based programs where precision is key, from accounting software
to project management tools.
Coders ki
Tipaniya:
Leap year ka
calculation hai coding ka chhota sa part, Career mein date validation mein ban
jaoge expert smart!
Option 1: C
Program to Check Leap Year Using if-else
#include
<stdio.h>
int main() {
// Declare an integer variable
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Check year is leap or not using if else
statement
if ((year % 4 == 0 && year % 100 !=
0) || (year % 400 == 0)) {
printf("%d is a leap year.",
year);
} else {
printf("%d is not a leap
year.", year);
}
return 0;
}
Run Code
Output:
Enter a
year: 2024
2024 is a
leap year.
Explanation:
Include
Standard I/O Library: #include <stdio.h> enables the use of printf() and
scanf() functions for input and output.
Variable
Declaration: Declare int year; to store the user-input year.
User Input:
scanf("%d", &year); reads the year entered by the user.
Leap Year
Conditions:
A year is a
leap year if it is:
Divisible by
4 but not divisible by 100, or
Divisible by
400.
The if-else
statement checks these conditions and determines if the year is a leap year.
Display
Result: printf() outputs whether the year is a leap year or not.
Time
Complexity Analysis
Time
Complexity : O(1)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 2: C
Program to Find Leap Year Using Function
#include
<stdio.h>
// Function
which return year is a leap year or not
int
isLeapYear(int year) {
return (year % 4 == 0 && year % 100
!= 0) || (year % 400 == 0);
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (isLeapYear(year)) {
printf("%d is a leap year.",
year);
} else {
printf("%d is not a leap
year.", year);
}
return 0;
}
Run Code
Output:
Enter a
year: 2025
2025 is not
a leap year.
Explanation:
This C
program to check leap year using a function creates isLeapYear(), which returns
1 for leap years and 0 otherwise. The main function uses this result to print
if the year is a leap year or not.
Time
Complexity Analysis
Time
Complexity : O(1)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 3: C
Program to Check Leap Year Using Conditional Operator
#include
<stdio.h>
int main() {
int year;
printf("Enter a year: ");
// Take year input from user
scanf("%d", &year);
// Check leap year using ternary operator
(year % 4 == 0 && year % 100 != 0)
|| (year % 400 == 0) ?
// If condition is true
printf("%d is a leap year.",
year) :
// If condition is false
printf("%d is not a leap
year.", year);
return 0;
}
Run Code
Output:
Enter a
year: 2026
2026 is not
a leap year.
Explanation:
This C
program to check leap year or not uses the ternary operator to evaluate the
leap year condition in a single line. It prints the appropriate message based
on the condition’s result.
Time
Complexity Analysis
Time
Complexity : O(1)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 4:
Program to Check Leap Year in C Using switch Statement
#include
<stdio.h>
int main() {
int year, leap;
printf("Enter a year: ");
scanf("%d", &year);
leap = (year % 4 == 0 && year % 100
!= 0) || (year % 400 == 0);
// Check year is a leap year or not
switch (leap) {
case 1:
printf("%d is a leap
year.", year);
break;
default:
printf("%d is not a leap
year.", year);
}
return 0;
}
Run Code
Output:
Enter a
year: 2000
2000 is a
leap year.
Explanation:
This C
program to find leap year or not uses a switch statement to evaluate the leap
year result stored in the leap variable. It then uses case and default to print
if the year is a leap year.
Time
Complexity Analysis
Time
Complexity : O(1)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 5:
Check Leap Year in C Using Recursion
#include
<stdio.h>
// Function
to check whether a year is a
// leap year
or not
int
isLeapYear(int year) {
// Return 1 (true)
// Otherwise return 0 (false)
return (year % 4 == 0 && year % 100
!= 0) || (year % 400 == 0);
}
// Function
to print the result based on the leap year check
void
checkLeapYear(int year) {
// Call isLeapYear function to check the
condition
if (isLeapYear(year)) {
// If true
printf("%d is a leap year.",
year);
} else {
// If false
printf("%d is not a leap
year.", year);
}
}
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Call function to check and print result
checkLeapYear(year);
return 0;
}
Run Code
Output:
Enter a
year: 2021
2021 is not
a leap year.
Explanation:
This program
shows how to check leap year in C using recursion by calling checkLeapYear()
with the isLeapYear() function inside, making it versatile for various program
structures.
Time
Complexity Analysis
Time
Complexity : O(1)
Space
Complexity Analysis
Space
Complexity : O(1)
Option 6: C
Program to Find Leap Year Using Nested If
#include
<stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Check if year is divisible by 4
if (year % 4 == 0) {
// If divisible by 4, check if
divisible by 100
if (year % 100 == 0) {
// If divisible by 100, check if
divisible by 400
if (year % 400 == 0) {
// Divisible by 400 → Leap year
printf("%d is a leap
year.", year);
} else {
// Divisible by 100 but not by
400 → Not a leap year
printf("%d is not a leap
year.", year);
}
} else {
// Divisible by 4 but not by 100 →
Leap year
printf("%d is a leap
year.", year);
}
} else {
// Not divisible by 4 → Not a leap year
printf("%d is not a leap
year.", year);
}
return 0;
}
Run Code
Output:
Enter a
year: 2020
2020 is a
leap year.
Explanation:
Outer if
(year % 4 == 0): Checks if the year is divisible by 4, the first condition for
a leap year.
First nested
if (year % 100 == 0): Checks if the year is also divisible by 100.
Second
nested if (year % 400 == 0): Checks if the year is divisible by 400, confirming
it as a leap year.
else inside
the second if: If not divisible by 400, it’s not a leap year.
else inside
the first if: If the year is divisible by 4 but not by 100, it’s a leap year.
else for the
outer if: If the year is not divisible by 4, it’s not a leap year.
Time
Complexity Analysis
Time
Complexity : O(1)
Space
Complexity Analysis
Space
Complexity : O(1)
No comments:
Post a Comment