C Program to check Leap Year

Language Used: C

Sreemeenakshi V
2 min readJun 3, 2021

Here are 2 most easy ways to Check if the given year is Leap Year or not. The Source Code along with its Output and Explanation are given below:

Using if else Ladder

What is if else ladder?

In C if_else_if ladder is used to help the user to decide from among multiple options. If the conditions controlling the if is True then the statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. If none of the conditions are true, then the final else statement will be executed.

Syntax for if-else ladder:

if (expression1) {
// statement(s)
}
else if(expression2) {
// statement(s)
}
else if (expression3) {
// statement(s)
}
.
.
else {
// statement(s)
}

Script

Output

Enter Year: 20002000 is a leap year.

Description:

Here we have used only if-else statement to check if the given year is a Leap Year. When the conditions are satisfied in the if statement, the following print statement is executed and if it fails, the else part is executed.

RETURN statement has returned an integer 0 since the main function started with “int” datatype.

Using Switch Case Statement

What is Switch and Case?

A switch statement allows a variable to tested for equality against a list of values where each value is called a Case and the variable that is being switched on is checked for each switch case. This can be used for faster execution through easier compiler optimization in many cases.

Syntax for Switch Case:

switch (expression)
​{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}

Flowchart:

Script

Output

Enter Year: 2028Leap Year.

Description:

Here, the variable remainder is used to proceed with the switch statement.

In C, the switch can have any number of case statements. It can also have a default statement which is optional. Here, default is used to print “Invalid” if it doesn’t satisfy the conditions or if none of the cases are true. It doesn’t need a break statement and appears at the end of the switch.

When the variable’s value gets equal to that of the case then the statements following the case will be executed until the break statement is reached.

The break statement when reached, the switch is terminated and the flow of control jumps to the next line following the switch statement.

For more solutions to your programming questions, Lab experiments and for other Educational Assistance, make sure to check out GUVI which serves as an amazing platform for students.

Refer: https://www.guvi.in/

--

--

No responses yet