top

Search

C Tutorial

.

UpGrad

C Tutorial

How to Find a Leap Year Using C Programming

Leap years are crucial in keeping our calendars synchronised with the Earth's orbit. By following logical conditions and employing mathematical calculations, we can accurately determine whether a year is a leap year. This practical guide will provide step-by-step explanations, code examples, and flowcharts to help you understand and implement leap year detection in your C programs. 

Before we delve into the world of C programming and understand how to calculate leap years, let’s first understand what a leap year is. 

What is a Leap Year?

A leap year is a particular year, according to the Gregorian Calendar, that occurs every fourth year. According to this system, a year is generally about 365.25 days. However, since we consider the number of days in a year to be 365, which leaves an extra 0.25 days extra each year. To account for this functional day, an additional day is added to the calendar every fourth year, making the year 366 days long instead of the regular 365 days. This extra day is generally inserted on the 29th of February. 

How to Find a Leap Year Using C Programming?

Here’s how to write a program to check leap year in C - 

#include <stdio.h>

int main() {
    int inputYear;

    printf("Enter a year: ");
    scanf("%d", &inputYear);

    if ((inputYear % 4 == 0 && inputYear % 100 != 0) || (inputYear % 400 == 0)) {
        printf("%d is a leap year.\n", inputYear);
    } else {
        printf("%d is not a leap year.\n", inputYear);
    }

    return 0;
}

Here’s how you can find a leap year using C language - 

  • The program prompts the user to enter a year.

  • The ‘scanf’ function is used to read the input from the user and store it in the variable ‘inputYear’.

  • The ‘if’ statement checks if the entered year satisfies the leap year conditions:

  • It should be divisible by 4 (inputYear % 4 == 0).

  • Alternatively, if the year is divisible by 400 (inputYear % 400 == 0) but not by 100 (inputYear % 100 != 0), it is also a leap year.

  • If the conditions are met, the program prints that the year is a leap year.

  • If the conditions are unmet, the program prints that the year is not a leap year.

  • This second condition is specially used to differentiate between century leap years from others. Century years are the ones that end in the 00s, like 1300, 1500, 1700, and more. A century year will only be considered a leap year if divisible by 400. Examples include years like 1200, 1600, 2000, and more. 

Pseudocode for a Leap Year C Program


// Prompt the user to enter a year
Print "Enter a year: "
Read year from user

// Check if the entered year is a leap year
if (inputYear is divisible by 4) then
    if (inputYear is divisible by 100) then
        if (inputYear is divisible by 400) then
            Print "It’s Leap year"
        else
            Print "It's Not a leap year"
        end if
    else
        Print "Leap year"
    end if
else
    Print "Not a leap year"
end if


How to Implement Leap Year Program in C?

Here’s how to implement leap year program in C - 

#include <stdio.h>

int main() {
    int inputYear;

    printf("Enter a year: ");
    scanf("%d", &inputYear);

    if (inputYear % 4 == 0) {
        if (inputYear % 100 == 0) {
            if (inputYear % 400 == 0) {
                printf("%d is a leap year.\n", inputYear);
            } else {
                printf("%d is not a leap year.\n", inputYear);
            }
        } else {
            printf("%d is a leap year.\n", inputYear);
        }
    } else {
        printf("%d is not a leap year.\n", inputYear);
    }

    return 0;
}

Output 1 - 

Here’s how you can check the leap year program in C in one line - 

Enter a year: 2020
2020 is a leap year.

2020 is divisible by 4, so it satisfies the first condition. It is not divisible by 100, which satisfies the second condition. Therefore, 2020 is a valid leap year.

Output 2 - 

Enter a year: 1900
1900 is not a leap year.

1900 is divisible by 4, so it satisfies the first condition. However, it is also divisible by 100, which fails the second condition. Therefore, 1900 is not a leap year, and the output is valid.

You can input different years and verify the outputs based on the leap year logic provided in the program.

C Program to Find Leap Years Within a Given Range

Let’s see how to write a leap-year program within a given range - 

#include <stdio.h>

int main() {
    int startYear, endYear;

    printf("Enter the starting year: ");
    scanf("%d", &startYear);

    printf("Enter the ending year: ");
    scanf("%d", &endYear);

    printf("Leap years within the range %d to %d:\n", startYear, endYear);

    for (int year = startYear; year <= endYear; year++) {
        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            printf("%d\n", year);
        }
    }

    return 0;
}

Output - 

Enter the starting year: 2000
Enter the ending year: 2024
Leap years within the range 2000 to 2024:
2000
2004
2008
2012
2016
2020
2024

In this example, the user enters the starting year as 2000 and the ending year as 2024. The program then calculates and displays all the leap years within that range. In this case, the leap years from 2000 to 2024 are printed: 2000, 2004, 2008, 2012, 2016, 2020, and 2024.

Write a Program to Check Leap Year in Java

Now that you have understood how to check leap year in C programming, here’s how you can write a program to check leap year in java -

import java.util.Scanner;

public class LeapYearChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a year: ");
        int inputYear = scanner.nextInt();

        if (isLeapYear(inputYear)) {
            System.out.println(inputYear + " is a leap year.");
        } else {
            System.out.println(inputYear + " is not a leap year.");
        }

        scanner.close();
    }

    public static boolean isLeapYear(int year) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}

To use this program, the user is prompted to input a year. The program then calls the ‘isLeapYear()’ method to check if the year is a leap year. The method evaluates the year based on the conditions for leap years: divisible by 4, not divisible by 100 (unless divisible by 400). The program outputs whether the year is a leap year or not.

To verify the leap year calculations, you can test this program with different input years.

Conclusion

The process of finding leap years using C programming, explained with the assistance of flowcharts, has provided us with a clear understanding of how to tackle it. Using logical conditions and mathematical calculations, we can accurately determine whether a year qualifies as a leap year. We can effectively identify the leap year program in c using if-else statements as well. This knowledge enables us to enhance our calendar-related calculations and accurately handle leap years. Let us embrace this newfound understanding and continue exploring the vast possibilities of C programming!

With upGrad’s Master of Science in Machine Learning & AI - Now with Generative AI Lectures, students can leverage their programming skills and learn all in-demand skills such as NLP, Deep Learning, Reinforcement Learning and more. Although this course is designed for working professionals, people from all walks of life are encouraged to join and learn something new. This will only help them advance their careers to become top-notch professionals in their particular fields. 

FAQs

1. Why do we have leap years?

Leap years are introduced to align our calendar with the Earth's orbit around the Sun, which takes approximately 365.25 days. By adding an extra day every four years, we account for the additional fractional day and keep our calendar in sync with the astronomical year

2. What happens if we don't have leap years?

Without leap years, our calendar would gradually drift out of sync with the seasons. Over time, this misalignment would cause significant seasonal variations and disruptions to activities dependent on the calendar, such as agriculture, astronomical observations, and scheduling of events.

3. How can the leap year of 2024 be determined?

To identify a leap year, it must be divisible by four, except for end-of-century years. However, end-of-century years that are divisible by 400 are considered leap years. Applying this rule, we find that 2024 is indeed a leap year.

Leave a Reply

Your email address will not be published. Required fields are marked *