Today we will learn leap year program in c and how to write a program for leap year in c. So before start learning, you should have a little bit of knowledge of leap year. So,
Table of Contents
What is Leap Year?
Generally, a normal year has 365 days but, a leap year has 366 days i.e 1 day extra in the month of February. The Leap year occurs every 4 years.
Logic: The year which is divisible by 4, and 400 is called a leap year.If year divisible by 4 but not divisible by 100 is also called a leap year.
The year not divisible by 4 and 100 but divisible by 400 is not a leap year.
There are many ways to check whether the year is a leap year or not. So, we will see one by one.
1. C program to check Leap Year Using if statement
In this program, we will ask the user to input year which user want to check whether the entering year is a leap year or not.
#include<stdio.h> void main() { int year; printf("Enter Year to check : "); scanf("%d",&year); 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); }
Output:
Program Logic:
In above program we have used a logic in if statement i.e (((year%4==0)&&(year%100!=0))||(year%400==0)).Here “||” is logical OR and “&&” is logical AND.
Our condition is (year%400==0).This condition will check whether the remainder is 0 or not. In the first condition (year%4) also check whether the remainder is 0 or not and if the condition gets FALSE then the program will exit.
Second Condition (year%100!=0).In this condition, if the remainder is not equal to 0 then it is a leap year. In Leap year definition we have defined that if the year which is divisible by 4 and 400 but not 100 is Leap Year.
2. Using else-if Statement
In this program, the user will enter any year and the program will check whether the year is leap year not using else-if statement.
#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) printf("%d is a leap year.\n", year); else if ( year%100 == 0) printf("%d is not a leap year.\n", year); else if ( year%4 == 0 ) printf("%d is a leap year.\n", year); else printf("%d is not a leap year.\n", year); return 0; }
Output:
In above program we have just divided the logic and declared it using else-if statement.But the logic behind the program is same.
3. Leap year program in c using Nested if Statement.
Now in this program, we will ask the user to enter the year and the program will perform its operations using nested if statements.
#include <stdio.h> int main() { int year; printf("\n Please Enter year to check \n"); scanf("%d",&year); if(year%4 == 0) { if( year%100 == 0) { if ( year%400 == 0) printf("\n%d is a Leap Year.", year); else printf("\n%d is not the Leap Year.", year); } else printf("\n %d is a Leap Year.", year ); } else printf("\n%d is not the Leap Year.", year); return 0; }
Output:
Condition 1st:(year%4) if the condition is false the given number is not leap year but the condition is true then we have to check for century year. So the compiler goes to the next condition.
Condition 2nd:(year%400) if the condition is false the year is not a leap. But, if the condition is true then the compiler will jump to the next condition.
Condition 3rd: This is the final condition which decides whether the entered year is a leap year or not. If (year%100!=0) then the year is a leap year, else year is not a leap.
4. Using Function
In this program we will use function is_leap_year to check whether the entered year is leap year or not.
#include <stdio.h> void is_leap_year(int year) { 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); printf("\n"); } int main() { int year; printf("\nEnter a year : "); scanf("%d",&year); printf("\n"); is_leap_year(year); return 0; }
Output:
In the above output images, we have entered the year 2020 then the output is a leap year because 2020 is divisible by 4 and (2020%100!=0).
Also Read:
- C Program to print Prime Numbers from 1 to n
- C Program to Make Simple Calculator
- Armstrong Number Program in C
- Fibonacci Series Program in C
- Decimal to Binary Conversion Program in C
- Reverse a String in C
- Program to Reverse a Number in C
- Hello World Program in C
- Palindrome Program in C
- Factorial Program in C
- Prime Number Program in C