Skip to content

Instantly share code, notes, and snippets.

@thearyanahmed
Created February 25, 2025 07:09
Show Gist options
  • Save thearyanahmed/81e76cbbb8a9e02bdb98cf3b56c56eeb to your computer and use it in GitHub Desktop.
Save thearyanahmed/81e76cbbb8a9e02bdb98cf3b56c56eeb to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main() {
int year, nearestLeap;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
printf("%d is a leap year.\n", year);
} else {
// Find the nearest leap year
if ((year + 1) % 4 == 0 && ((year + 1) % 100 != 0 || (year + 1) % 400 == 0)) {
nearestLeap = year + 1;
} else if ((year - 1) % 4 == 0 && ((year - 1) % 100 != 0 || (year - 1) % 400 == 0)) {
nearestLeap = year - 1;
} else if ((year + 2) % 4 == 0 && ((year + 2) % 100 != 0 || (year + 2) % 400 == 0)) {
nearestLeap = year + 2;
} else {
nearestLeap = year - 2;
}
printf("%d is not a leap year. Nearest leap year is %d.\n", year, nearestLeap);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment