Program in C language to check whether a year is a leap year or not a leap year using if-else. In this article, we will create a program to check whether a year is a leap year or not. If you want to learn the ‘C’ language check this tutorial https://codedec.com/course/c-programming-tutorial/
What is a leap year?
As you all know, a year that occurs after every four years and which has 366 days is called a Leap year.
Algorithm
- Declare a variable year.
- After that, take input with the help of scanf function.
- Check the following condition
- If the year is divisible by 400, it is a leap year.
- If the year is not divisible by 400, but by 100, it is not a leap year.
- If the year is not divisible by 100, but by 4, it is a leap year.
- Then print output with a suitable/appropriate message.
C program to check whether a year is leap year or not a leap year
#include<stdio.h> void main(){ int a; printf("enter a value"); scanf("%d",&a); if(a % 400 == 0){ printf("%d is a leap year",a); } else if (a % 100 == 0) { printf("%d is not a leap year",a); } else if (a % 4 == 0) { printf("%d is a leap year",a); } else { printf ("%d is not a leap year",a); } }
Explanation of the source code
Step 1: Start.
Step 2: Add header files of Standard Input/Output using #include.
Step 3: Then create the main function method which is the starting point of the program execution.
Step 4: After that, declare a variable as “int a”.
Step 5: Take an input with the help of scanf function. Put & before variable in scanf() function.
Step 6: Then check the following conditions
- First condition is if (a%400==0) then print using printf() method.
- Second condition is if else (a%100==0) then print using printf() method.
- Third condition is if else (a%4==0) then print using printf() method.
- The fourth and last condition is else then print using printf() method.
Step 7: Now execute the program and check whether it is a leap year or not a leap year with a suitable message.
Step 8: End.
Output
Thus, this is how we create a program in C to check whether a year is a leap year or not using the if-else block.