Leap year program in C
×


Leap year program in C

186

The C program to determine if a given year is a leap year or not checks specific conditions.

A leap year is one that is divisible by 4 but not by 100 unless it's also divisible by 400.

The program employs conditional statements to decide the leap year status based on user-provided input.

Program 1: Using Basic Conditional Statements 

// Program for Leap year in C Using basic conditional Statements
#include<stdio.h>

int main() {
    int A_year;

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

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

    return 0;
}

Output:

Enter a year: 2020
2020 is a leap year.

This program uses basic conditional statements to check for leap-year conditions.

It first checks if the year is divisible by 4 but not by 100.

If this condition isn't met, it checks if the year is divisible by 400 to determine if it's a leap year.

Program 2: Using Ternary Operator  

// Program for Leap Year in C using Ternary Operator
#include<stdio.h>

int main() {
    int A_year;

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

    (A_year % 4 == 0 && A_year % 100 != 0) || (A_year % 400 == 0) ? 
    printf("%d is a leap year.\n", A_year) : 
    printf("%d is not a leap year.\n", A_year);

    return 0;
}

Output:

Enter a year: 2024
2024 is a leap year.

This program utilizes the ternary operator to check for leap year conditions in a more compact form.

If the condition for a leap year is true, it prints that the year is a leap year; otherwise, it prints that it's not a leap year.



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments