“Is 2024 a Leap Year?”, Here’s how to check, using C language.
Tags: c, tutorial, code, programming
Published: 2024-01-17 09:49:15

## Introduction

What is a leap year?:

A leap year is a year that is a day longer than other years, i.e. it has 366 days instead of 365 days.

When is the extra day?:

In a leap year, the extra day is the 29th of February, instead of the normal 28 days. This extra day brings the total number of days to 366 days.

Why do leap years exist?:

Leap years exist to adjust our calendar system. To keep it synchronized with the Earth's orbit around the Sun.

In this article, you’ll write a program in C to check if a year is a leap year, or not.

## Prerequisite

- Understanding of the basics of C programming and its syntax.
- An integrated development environment is set up on your machine.
- Basic knowledge of your integrated development environment.

## Terminologies to understand to aid with this lesson

The rules for leap years are:

- If a year is divisible by 4 without remainder, it is a leap year.
- For centurial years, if it is divisible without remainder by 100 but not 400, it is not a leap year.

## Steps

Let’s begin, you’ll create a program in C language to check if a year is a leap year.

### Step 1 - Set up your IDE(Integrated Development Environment)

Create a file in your IDE that’ll store your source code. This is a fundamental step in establishing a well-organized and efficient development environment.

### Step 2 - Header File

Include the standard input/output library.

```c
#include <stdio.h>
```

`#include <stdio.h>` includes the standard input/output library. Which is necessary to use functions like `printf` and `scanf`.

### Step 3 -Declare Variable

Declare a variable.

```c
int year;
```

An integer variable named `year` is declared. This variable will store the user-inputted year.

### Step 4 - User-Input

Code to accept user input is required. 

```c
printf("Enter year: ");
scanf("%d", &year);
```

`printf("Enter year: ");` prompts the user to enter a year. While `scanf("%d", &year);` reads the inputted year and stores it in the variable `year`.

### Step 5 - Leap Year Check

Next, you’ll write your code to check if the year inputted is a leap year or not.

####Step 5.1 - If Statement** 

```c
if (year % 400 == 0)
{
    printf("%d is a leap year", year);
}
```

The `if` statement checks if the inputted year is divisible by 400 without remainder. If it is, a message indicating it is a leap year will be printed.

####Step 5.2 - else if statement**

```c
else if (year % 4 == 0)
{
    printf("%d is a leap year", year);
}
```

If the condition is not met in the first `if` statement, the program moves to the `else if` statement. if the inputted year is divisible by 4, it’ll print a message indicating it is a leap year.

####Step 5.3 - else statement**

```c
else
{
    printf("%d is not a leap year", year);
}
```

If both conditions in the `if` and `else if` statements are not satisfied, the program executes the `else` statement. This means the year inputted is not divisible by 400 and 4 without remainder. A message indicating the year is not a leap year will be printed in this case.

```c
return 0;
}
```

This indicates the successful execution of the program.

### Step 6 - Execute Code

Now, you’ll check if **2024** is a leap year or not.

In your IDE, run your code and input 2024 as the user input when prompted to “Enter year:”.

As 2024 can be divided by 4 without remainder, your terminal would return the output below:


![Terminal output showing "2024 is a leap year"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pjm1zrpj3ox1305p8scq.png)

Let’s see what happens when we enter **2023** when prompted to “Enter year”.

As 2023 cannot be divided by 400 or 4 without remainder, the output below will be printed on your terminal:


![Terminal output showing "2023 is not a leap year"](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9jy3q5pl8imzp8uvrq9.png)

## Conclusion

Congratulations!, you’ve successfully run a program to check if 2024 is a leap year. By utilizing some basic syntax in C, you’ve gained experience with how functions like `printf`, `scanf`, `if`, `else if`, and `else` statements work.

If you enjoyed this article, please follow, like, and leave a comment. Let’s connect on [LinkedIn](http://www.linkedin.com/in/abdulsalam-yusuf-090b63238).

Happy Coding!