Leap Year Program in C

Introduction

In this post, I am going to write a leap year program in c. I have also explained many programs which are same as that of the leap year program. But, before that let us find out how to calculate leap year.

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

How to Calculate Leap Year?

There are two types of years. One is a century year and another is a non-century year. The century year is nothing but a year which is divisible by 100 (E.g. 200, 800 etc. ) and other years are non-century years.

So we can define, leap year in two ways.

  1. If a century year is divisible by 400 and 4, then that year is called a leap year. For example, 2000 is a leap year but 1900 is not a leap year.
  2. If a non-century year is divisible by 4 only then that year is a leap year. For example, 2020 is a leap year but 1997 is not a leap year.

Algorithm

  1. Start
  2. Read the year.
  3. If the year is divisible by 400 then print Leap Year and exit.
  4. Else if the year is divisible by 100 then print Not a Leap Year and exit.
  5. Again check if the year is divisible by 4 then print Leap year and exit.
  6. If all the above steps are false then print Not a Leap Year.
  7. Stop

Also Read: C Language Program to Count the Number of Lowercase Letters in a Text File

Program in C to Find a Leap Year

This is a very simple program. We just have to use the if-else statement to write this c program.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int year;
    printf("Enter any year\n");
    scanf("%d",&year);
    if(year%400==0)
    {
       printf("%d is a Leap Year",year);
    }
    else if(year%100==0)
    {
        printf("%d is Not a Leap Year",year);
    }
    else if(year%4==0)
    {
        printf("%d is a Leap Year",year);
    }
    else
    {
        printf("%d is a Leap Year",year);
    }
    return 0;
}

Output

Enter any year
2020
2020 is a Leap Year

Enter any year
1900
1900 is Not a Leap Year

Related Programs

C Program to Find Leap Year Using Function

In this program, I am using function to find a leap year.

Also Read: C Program to Remove White Spaces and Comments from a File

#include <stdio.h>
#include <stdlib.h>
int main()
{
    void leapyear(int);
    int year;
    printf("Enter any year\n");
    scanf("%d",&year);
    leapyear(year);
    return 0;
}
void leapyear(int year)
{
    if(year%400==0)
    {
       printf("%d is a Leap Year",year);
    }
    else if(year%100==0)
    {
        printf("%d is Not a Leap Year",year);
    }
    else if(year%4==0)
    {
        printf("%d is a Leap Year",year);
    }
    else
    {
        printf("%d is a Leap Year",year);
    }
}

Leap Year Program in C using Conditional Operator

In this section, I am going to write the same program but we have to use a conditional operator. To know more about the conditional operator, read operators in c programming.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int year;
    printf("Enter any year\n");
    scanf("%d",&year);
    (((year % 4 == 0) && (year % 100!= 0)) || (year%400 == 0))?printf("%d is a Leap Year",year):printf("%d is Not a Leap Year",year);;
    return 0;
}

The output of this program is same as the first program.

Also Read: C Program to Find the Sum of Cubes of Elements in an Array

C Program to Display the Leap Year in a Given Range using for loop

In this program, we are printing leap years between a given range using for loop.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,y1,y2;
    printf("Enter the range\n");
    scanf("%d%d",&y1,&y2);
    printf("Leap years between %d and %d are\n",y1,y2);
    for(i=y1;i<=y2;i++)
    {
        if(((i%4==0)&&(i%100!=0))||(i%400==0))
        {
            printf("%d ",i);
        }
    }
    return 0;
}

Output

Enter the range
1890
1920
Leap years between 1890 and 1920 are
1892 1896 1904 1908 1912 1916 1920

C Program to Display the Leap Year in a Given Range using while loop

In this program, we are printing leap years between a given range using while loop.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,y1,y2;
    printf("Enter the range\n");
    scanf("%d%d",&y1,&y2);
    printf("Leap years between %d and %d are\n",y1,y2);
    i=y1;
    while(i<=y2)
    {
        if(((i%4==0)&&(i%100!=0))||(i%400==0))
        {
            printf("%d ",i);
        }
        i++;
    }
    return 0;
}

Write a Program to Print Leap Year Between 1900 to 2000 in C

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i;
    printf("Leap years between 1900 and 2000 are\n");
    for(i=1900;i<=2000;i++)
    {
        if(((i%4==0)&&(i%100!=0))||(i%400==0))
        {
            printf("%d ",i);
        }
    }
    return 0;
}

Output

Leap years between 1900 and 2000 are
1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980 1984 1988 1992 1996 2000

Write a C Program to display the Leap Years in Between 1998 to 2020

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i;
    printf("Leap years between 1998 and 2020 are\n");
    for(i=1998;i<=2020;i++)
    {
        if(((i%4==0)&&(i%100!=0))||(i%400==0))
        {
            printf("%d ",i);
        }
    }
    return 0;
}

Output

Leap years between 1998 and 2020 are
2000 2004 2008 2012 2016 2020

How to Calculate Number of Leap Years Between Two Years in C

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,y1,y2,count_leap=0;
    printf("Enter the range\n");
    scanf("%d%d",&y1,&y2);
    for(i=y1;i<=y2;i++)
    {
        if(((i%4==0)&&(i%100!=0))||(i%400==0))
        {
            count_leap++;
        }
    }
    printf("Total Number of Leap Years between %d and %d are %d",y1,y2,count_leap);
    return 0;
}

Output

Enter the range
2000
2999
Total Number of Leap Years between 2000 and 2999 are 243

C Program to Print Next 20 Leap Years

In this program, we will read any year from the user and then print the next 20 leap years. See the following program.

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i,year,count=0;
    printf("Enter any year\n");
    scanf("%d",&year);
    printf("Next 20 Leap Years from %d are \n",year);
    i=year;
    while(1)
    {
        if(((i%4==0)&&(i%100!=0))||(i%400==0))
        {
            printf("%d ",i);
            count++;
        }
        if(count==20)
        {
            break;
        }
        i++;
    }
    return 0;
}

Here, I have written while loop which will be executed infinite times. But, I have used here the keyword break. The break terminates the loop when executes. So, when the value of the variable counter becomes 20, then the control will come out of the loop and we will get our output.

Enter any year
1900
Next 20 Leap Years from 1900 are
1904 1908 1912 1916 1920 1924 1928 1932 1936 1940 1944 1948 1952 1956 1960 1964 1968 1972 1976 1980

These are all the c programs for leap years. I hope you have understood all the programs. If you have doubt or any feedback, please feel free to contact me.

Thank you.

FAQs

Leave a Comment