Leap Year: flowchart, algorithm and program

This program reads a year and prints whether it is a leap year. It uses one combined condition: a year is leap if it is divisible by 4 but not by 100, or if it is divisible by 400.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read year.
  3. Step 3: If (year%4==0 && year%100!=0) || year%400==0, go to step 4; otherwise go to step 5.
  4. Step 4: Print year is a leap year. Go to step 6.
  5. Step 5: Print year is not a leap year.
  6. Step 6: Stop.

Pseudocode

BEGIN
    READ year
    IF (year%4==0 && year%100!=0) || year%400==0 THEN
        PRINT year is a leap year
    ELSE
        PRINT year is not a leap year
    END IF
END

Program in C, Python and Java

Leap Year program in C

#include <stdio.h>

int main() {
    int year;
    printf("Enter year: ");
    scanf("%d", &year);
    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        printf("%d is a leap year\n", year);
    else
        printf("%d is not a leap year\n", year);
    return 0;
}

Leap Year program in Python

year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Leap Year program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter year: ");
        int year = sc.nextInt();
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
            System.out.println(year + " is a leap year");
        else
            System.out.println(year + " is not a leap year");
    }
}

Sample input: year = 2024
Output: 2024 is a leap year

How it works

  • scanf stores the number the user types in the variable year.
  • The first part, year % 4 == 0 && year % 100 != 0, catches normal leap years like 2024 or 2016: divisible by 4 but not a century year.
  • The second part, year % 400 == 0, catches century years that are still leap years, like 2000. The || joins the two parts, so only one of them needs to be true.
  • If the whole condition is true, the if branch prints 'is a leap year'; otherwise the else branch prints 'is not a leap year'.
  • Dry run with year = 2024: 2024 % 4 is 0 and 2024 % 100 is 24 (not 0), so the first part is true and the program prints '2024 is a leap year'.

Dry run (trace table)

StepLineBoxyearOutput
13Start
26Read year2024Enter year:
37(year%4==0 && year%100!=0) || year%400==0?2024
48Print year is a leap year20242024 is a leap year
512Stop2024

Time and space complexity

Time: O(1) · Space: O(1). Only a fixed number of arithmetic comparisons is done, regardless of the year's value.

Common mistakes

  • Checking only year % 4 == 0. This wrongly calls 1900 a leap year, because century years must also be divisible by 400.
  • Using && instead of || between the two parts. Then 2000 would fail, since 2000 % 100 is 0.
  • Writing year % 4 = 0 with a single = (assignment) instead of == (comparison), or leaving out the brackets around the && part and getting confused about precedence.

Viva questions

Q1. What is a leap year, and what rule does your condition on line 7 implement?

A leap year has 366 days. A year is leap if it is divisible by 4 but not by 100, or if it is divisible by 400.

Q2. What does the % operator do, and why do we use it here?

% gives the remainder after integer division. If year % 4 is 0, the year divides exactly by 4, so we use it to test divisibility.

Q3. Why is year % 100 != 0 needed? Why not just check year % 4 == 0?

Century years like 1900 are divisible by 4 but are not leap years. The check for 100 removes them, and the check for 400 adds back years like 2000.

Q4. Why are 1900 and 2000 treated differently? Trace both through the condition.

1900: divisible by 4 but also by 100, and not by 400, so the condition is false and it is not leap. 2000: divisible by 400, so the second part is true and it is leap.

Q5. What is the role of the brackets and the || and && operators in line 7?

&& binds tighter than ||, but the brackets make the grouping clear: (div by 4 AND not by 100) OR (div by 400). Without the grouping the logic could be misread.

Q6. What does scanf do here and why is &year used?

scanf reads an integer typed by the user and stores it in year. It needs the address of year (&year) so it can write the value into that variable.

Q7. What happens for edge inputs like 0 or a negative year?

The program doesn't validate input. 0 % 400 == 0, so 0 is reported as leap. Negative years work in C because the remainder is 0 whenever the year divides exactly. The Gregorian calendar itself does not define such years, though.

Q8. What is the time complexity of this program?

O(1), constant time. It does a fixed number of modulus operations and comparisons regardless of the value of the year.

Q9. What would change if you wanted to reject invalid years (year <= 0) or check many years in a range?

For validation, add an if before line 7 that prints an error when year <= 0. For a range, wrap the check in a for loop, which would make the complexity O(n) for n years.

Frequently asked questions

Why is 1900 not a leap year but 2000 is?

1900 is divisible by 100 but not by 400, so it fails both parts of the condition. 2000 is divisible by 400, so the second part is true.

Why do we need both the 100 and the 400 checks?

Most years divisible by 100 are not leap years, but those divisible by 400 are an exception. The 100 check removes the normal century years and the 400 check brings back the exceptions.

Are the brackets in the if condition really needed?

The && already binds tighter than ||, so it would work without them, but the brackets make the grouping clear and are good practice.

More programs

← All programs

Try AI Flowchart free →