C Program to Check Whether a given Year is a Leap Year

«
»

This is a C program to find whether a given year is leap year or not.

Problem Description

This program takes a year as input and finds whether a year is leap year or not.

Problem Solution

1. Take a year as input.
2. Check whether a given year is divisible by 400.
3. Check whether a given year is divisible by 100.
4. Check whether a given year is divisible by 4.
5. If the condition at step 2 and 4 becomes true, then the year is a leap year.
6. If the condition at step 3 becomes true, then the year is not a leap year.

advertisement
Program/Source Code

Here is source code of the C program to check a given year is leap year. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to find whether a given year is leap year or not
  3.  */
  4. void main()
  5. {
  6.     int year;
  7.  
  8.     printf("Enter a year \n");
  9.     scanf("%d", &year);
  10.     if ((year % 400) == 0)
  11.         printf("%d is a leap year \n", year);
  12.     else if ((year % 100) == 0)
  13.         printf("%d is a not leap year \n", year);
  14.     else if ((year % 4) == 0)
  15.         printf("%d is a leap year \n", year);
  16.     else
  17.         printf("%d is not a leap year \n", year);
  18. }
Program Explanation

1. Take a year as input and store it in the variable year.
2. Using if,else statements to,
a) Check whether a given year is divisible by 400.
b) Check whether a given year is divisible by 100.
c) Check whether a given year is divisible by 4.
3. If the condition at step 2.a becomes true, then print the ouput as “It is a leap year”.
4. If the condition at step 2.b becomes true, then print the ouput as “It is not a leap year”.
5. If the condition at step 2.c becomes true, then print the ouput as “It is a leap year”.
6. If neither of the condition becomes true, then the year is not a leap year and print the same.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement
Runtime Test Cases
 
Enter a year
2012
2012 is a leap year
 
 
Enter a year
2009
2009 is not a leap year

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

Check this: BCA MCQs | C Books
advertisement
If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.