A Leap year occurs only once in four years. Today we will create a C program to check leap year In this program we will use if-else statements to match the conditions. So let’s start creating a program for the leap year.
C Program to Check Leap Year:
First of all, declare a variable here we are using “year” as a variable. Now ask the user to enter a year. For this, we will use printf statement. To get value from the user we will use scanf function.
After taking the value from the user, we will use if-else statements. A decision control statement checks the conditions which are applied, if the condition is true, it will execute the body of if, if the condition of the if statement is not true then it will check the condition of the else if statement .If the condition of else if is true, then it will execute its body, and if the condition is false then it will check for the next else if statement.
In the first condition, we use the condition(year%400==0) if the year is totally divisible by 400 then the year entered by the user is a leap year. we have used the modulus operator(%), which is used to give the remainder of a number.
The year which is used in the condition is input by the user, and the equality operator(==) is used in the condition is used to check whether the remainder is equal to zero(0) or not, and if the remainder is equal to zero then the condition will be true.
The same is the case for the next else if statement, in the next else if statement, we have used the condition(year%100==0), in which the user has to enter the year, if the year is totally divisible by 100 and the remainder is equal to zero, then it will print the year which will be a leap year. and if the condition is not true, then it will move to the next else if statement.
The same is the case for the next else if statement, the condition is used in this statement is (year%4==0), in this condition, if the entered year is not divisible by 400 and 100, then we will apply the condition of 4, as we know, leap year come once in 4 years. the compiler will check the condition for 4, if the year entered by the user is divisible by 4, and the remainder is equal to 0, then the year will be a leap year.
In the end, we have used the else statement, which is executed when all the conditions of the if and else if statements will be false. If the entered year is not a leap then else condition will be executed. You can also check the if-else programs in our websites.
Program:
//C Program to Check Leap Year #include<stdio.h> int main() { int year; printf("Enter the year to check whether the year is leap or not\n"); //First You Have To Enter The Year You Want To Check That The Entered Year is Leap Year Or Not// scanf("%d",&year); /*Here we use if else statement And In if else statement we have to give the condition We Use Modulus Operator(%)*/ //leap Year if the year is divisible by 400// if(year%400==0){ printf("%d is a leap year\n",year); } //if year is not divisible by 400 but divisible by 100// else if(year%100==0) { printf("%d is a leap year\n",year); } /*if year is not divisible by 400 and 100 but divisible by 4*/ else if(year%4==0){ printf("%d is a leap year\n",year); } //if year is not divisible by 4 ,100 and 400 then entered year is not leap year// else printf("%d is not a leap year\n",year); return 0; }
OUTPUT:
Conclusion:
From the above article, we have concluded how to write a C Program to Check Leap Year We have done this program by decision-control statements. If the condition is true then entered year is a leap year and if the condition is false then the year entered by the user is not a leap year.
[sc_fs_faq html=”true” headline=”h4″ img=”” question=”Why do we divide by 400 to check leap year?” img_alt=”” css_class=””] a century year cannot be a leap year unless it is divisible by 400. that’s why we divide be 400 to check that entered year is a leap year or not [/sc_fs_faq]
[sc_fs_faq html=”true” headline=”h4″ img=”” question=”What is a leap year easy?” img_alt=”” css_class=””] The year that has an extra day in the month of February. [/sc_fs_faq]