Checking for leap years in C: A simple program


It is always legal in C programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s). If else statements are used for decision making, by specifying which block of code is to be executed when a certain condition is met. Nested if else statements are just if else statements inside other if else statements to provide better decision making.

Syntax:
if(Expression 1)
{
    // Executes when the Expression 1 is true
     if(Expression 2)
     {
        // Executes when the Expression 2 is true
     }
     else
     {
        // Executes when the Expression 2 is false
     }
}

This program is used to check if a given year is a leap year or not. The program first prompts the user to input a year using the "scanf" function. The input value is stored in the variable "year". The program then uses nested if-else statement to check if the year is a leap year or not. First, it checks if the year is divisible by 100, if yes then it checks whether year is divisible by 400 or not. If it is divisible by 400, it is a leap year, otherwise it is not. If the year is not divisible by 100, then it checks if the year is divisible by 4 or not. If it is divisible by 4, it is a leap year, otherwise it is not. The program then prints whether the given year is a leap year or not. Finally, it returns 0.


Source Code

// Nested if Finding given year is leap year
 
#include<stdio.h>
int main()
{
    int year;
    printf("\nEnter The Year : ");
    scanf("%d",&year);
    if(year%100==0)
    {
        if(year%400==0)
        {
            printf("%d is leap Year",year);
        }
        else
        {
            printf("%d is Not leap Year",year);
        }
    }
    else
    {
        if(year%4==0)
        {
            printf("%d is leap Year",year);
        }
        else
        {
            printf("%d is Not leap Year",year);
        }
 
    }
    return 0;
}
 
To download raw file Click Here

Output

Enter The Year : 1900
1900 is Not leap Year
Enter The Year : 2000 2000 is leap Year

List of Programs


Sample Programs


Switch Case in C


Conditional Operators in C


Goto Statement in C


While Loop Example Programs


Looping Statements in C

For Loop Example Programs


Array Examples in C

One Dimensional Array


Two Dimensional Array in C


String Example Programs in C


Functions Example Programs in C