Leap year program in C
C program to check leap year. This program takes a year as input and finds whether a year is the leap year or not. C Program to Find if a given Year is a Leap Year
We generally assume that if a year number is evenly divisible by 4 is the leap year. But it is not the only case. A year is a leap year if −
- It is evenly divisible by 100
- If it is divisible by 100, then it should also be divisible by 400
- Except this, all other years evenly divisible by 4 are leap years.
Let’s see how to we can create a program to find if a year is a leap or not
1 |
#include <stdio.h>int main(){int year;printf("Enter a year to check if it is a leap year\n");scanf("%d", &year);if ( year%400 == 0) // Exactly divisible by 400 e.g. 1600, 2000printf("%d is a leap year.\n", year);else if ( year%100 == 0) // Exactly divisible by 100 and not by 400 e.g. 1900, 2100printf("%d is not a leap year.\n", year);else if ( year%4 == 0 ) // Exactly divisible by 4 and neither by 100 nor 400 e.g. 2016, 2020printf("%d is a leap year.\n", year);else // Not divisible by 4 or 100 or 400 e.g. 2017, 2018, 2019printf("%d is not a leap year.\n", year);return 0;}</stdio.h> |
1 |
#include <stdio.h>int main() {int year;year = 2016;if (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))printf("%d is a leap year", year);elseprintf("%d is not a leap year", year);return 0;}</stdio.h> |