Program to check leap year in C language
- Home
- Check value
- Program to check leap year in C language
- On
- By
- 0 Comment
- Categories: Check value, If block
Program to check leap year in C language
Program to check leap year in C language
In this article, we will discuss the concept of Program to check leap year in C language
In this post, we are going to learn how to write a program to check and display the given year is leap or not in C programming language.
What is leap year
leap year is occurring once every four years, which has 366 days including 29 February as an intercalary day. it is called leap year
Ex – 2000,2004.2008
Century year
The century year is ending with 00
Ex 2000, 2100, 2200
a century year may be a leap year(only if the year is exactly divisible by 400)
How to find the leap year
if The given year is perfectly divisible by 4 , it is a leap year(except century year)
only if is perfectly divisible by 400, the century year is a leap year,
a century year should be divisible by 4 and 100 both
a non century year should be divisible only by 4
Code to check leap year using if else
Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – else statements in C language
Program 1
#include <stdio.h> #include <stdlib.h> int main() { int year; //variable declaration printf("Enter a year for check leap or not\n"); //Ask input from the user scanf("%d",&year); //store the input in the year variable if ((year%400==0)||((year%4==0)&&(year%100!=0))){ printf("%d is a leap year ",year); }//display leap year else{ printf("%d is not a leap year ",year); } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a year for check leap or not 2404 2404 is a leap year
Case 2
Enter a year for check leap or not 2200 2200 is not a leap year
Code to check leap year using if else if
Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using if – else-if else statements in C language
Program 2
#include <stdio.h> #include <stdlib.h> int main() { int year; printf("Enter a year for check leap or not\n"); //ask input from the user scanf("%d",&year); //store the year in the years variable entered by user if(year%400==0){// if the year is perfectly divisible by 400 //it is a leap year printf("%d is a leap year ",year); }//if the year is divisible by 100 , it is not leap year else if(year%100==0){ printf("%d is not a leap year ",year); } //if a year is divisible by 4 but not divisible by 100 //it is a leap year else if(year%4==0){ printf("%d is a leap year ",year); } else{ printf("%d is not a leap year ",year); } getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a year for check leap or not 2400 2400 is a leap year
Case 2
Enter a year for check leap or not 2500 2500 is not a leap year
Code to check leap year using Nested if
Here, The program allows the user to enter the year and then it will check the given year is a leap year or not, using Nested if – else statements in C language
Program 3
#include <stdio.h> #include <stdlib.h> int main() { int year;//variable declaration printf("Enter a year for check leap or not\n"); //Ask input from the user scanf("%d",&year); //store the value in the year variable 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(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a year for check leap or not 2020 2020 is a leap year
Case 2
Enter a year for check leap or not 2700 2700 is not a leap year
Code to check leap year using ternary operator
Here, The program allows the user to enter the year and then it will check the given year is a leap year or not using Ternary operator in C language
Program 4
#include <stdio.h> #include <stdlib.h> int main() { int year;//variable declaration printf("Please Enter year for check leap: \n"); //Ask input from the user scanf("%d",&year); //Store the input in the year variable (year%4==0 && year%100!=0)? printf("The entered year is a leap year"):(year%400==0)? printf("The entered year is a leap year") :printf("This is not a leap year"); getch(); return 0; }
When the above code is executed, it produces the following result
Case 1
Enter a year for check leap or not 2024 The entered year is a leap year
Case 2
Enter a year for check leap or not 2022 The entered year is not a leap year
Suggested post
Nested if statements in C language
input output function in C language
Similar post
Java program to find leap year
Python program to find leap year