Leap Year Program in C | Program for Leap Year in C
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.
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.
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.
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.
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).
Post a Comment