×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C program to print all Leap Year from 1 to N

This program will read value of N and print all Leap Years from 1 to N years. There are two conditions for leap year: 1- If year is divisible by 400 ( for Century years), 2- If year is divisible by 4 and must not be divisible by 100 (for Non Century years).

Print all Leap Years from 1 to N using C program

/*C program to print all leap years from 1 to N.*/
 
#include <stdio.h>
 
//function to check leap year
int checkLeapYear(int year)
{
    if( (year % 400==0)||(year%4==0 && year%100!=0) )
        return 1;
    else
        return 0;
}
 
int main()
{
    int i,n;
 
    printf("Enter the value of N: ");
    scanf("%d",&n);
 
    printf("Leap years from 1 to %d:\n",n);
    for(i=1;i<=n;i++)
    {
        if(checkLeapYear(i))
            printf("%d\t",i);
    }
     
    return 0;
}

Output

Enter the value of N: 2000
Leap years from 1 to 2000:
4       8       12      16      20      24      28      32      36      40
44      48      52      56      60      64      68      72      76      80
84      88      92      96      104     108     112     116     120     124
128     132     136     140     144     148     152     156     160     164
168     172     176     180     184     188     192     196     204     208
...
1532    1536    1540    1544    1548    1552    1556    1560    1564    1568
1572    1576    1580    1584    1588    1592    1596    1600    1604    1608
1612    1616    1620    1624    1628    1632    1636    1640    1644    1648
1652    1656    1660    1664    1668    1672    1676    1680    1684    1688
1692    1696    1704    1708    1712    1716    1720    1724    1728    1732
1736    1740    1744    1748    1752    1756    1760    1764    1768    1772
1776    1780    1784    1788    1792    1796    1804    1808    1812    1816
1820    1824    1828    1832    1836    1840    1844    1848    1852    1856
1860    1864    1868    1872    1876    1880    1884    1888    1892    1896
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

C Looping Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.