EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Leap Year Program in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Leap Year Program in C

Leap Year Program in C

Introduction to Leap Year Program in C

Generally, as we already know a year has 365 days but leap year consists of 366 days. This one day is added in the month of February. This month which generally has 28 days and also known as the shortest month in a year would get added with an extra day, which gives us a total of 29 days in that month. It is based on the Georgian calendar.

Here, let us see how to write a program for checking out whether a given year is a leap year or not in C programming language.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Logic:

  • Any year which is divisible for 4, and divisible by both 100 and 400 then it is a leap year.
  • The year which is divisible by 4 and not divisible by 100 then that year would be a leap year.
  • And obviously, if the year is divisible by 4 and 100 but not divisible by 400 then that year would not be called a leap year.

Pseudocode/Algorithm:

Let us have the above-written logic in the form of a pseudo-code in an algorithmic written format:

  • If the year is divisible for 400 alone. Then it is a leap year. If not, then it is a non-leap year.
  • Else if the year given is only divisible by 100, then it is a non-leap year.
  • Else if the same year is a leap year if the given year is completely divisible by 4.

Flowchart

Below, let us see the programming of leap year through a small flowchart:

Flowchart

The main condition lies with the year value of divisibility with 4 and 100 and the other condition is 400.

#include <stdio.h>
int main()
{
int r;
printf("Enter any year value: ");
scanf("%d",&r);
if( ( (r%4 == 0) && (r%100 !=0) ) || (r%400 == 0))
{
printf("This year is definitely a leap year");
}
else
{
printf("This year is not at all leap year");
}
}

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Output:

leap year 4

We can have an output for the non-leap year too:

not at all leap year

Above here, we have got all the conditions if the year values in a single if statement by using ‘and’ and ‘or’ statements.

Examples of Leap Program in C

Let us see the example below:

Example #1

#include <stdio.h>
int main()
{
int year=2020;
if(year % 4 == 0)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
{
printf("The given year 2020 is a leap year");
}
else
{
printf("The given year 2020 is not a leap year");
}
}
else
{
printf("The given year 2020 is a leap year");
}
}
else
{
printf("The given year 2020 is not a leap year");
}
}

Output:

leap year program in c 1

  • Here, the main and the first test would be, if the year given is being divisible by 4 or not.
  • There is an if-else condition for the divisibility test for 4.
  • If the condition gives the output true, then there would be next ‘if statements’ are true or not.
  • If the divisibility test 100 condition becomes true then the test for 400 is being done.
  • If that divisibility test is passed for 100 then the divisibility test for 400 should be done successfully too.
  • Else, that year would not be considered as a leap year.
  • And obviously, if the first step of divisibility by 4 has to be successful else at that point itself, we can consider that as a non-leap year condition.

Example #2

#include <stdio.h>
int main()
{
int y;
printf("Enter any year: ");
scanf("%d", &y);
if(y % 4 == 0)
{
if(y % 100 == 0)
{
if(y % 400 == 0)
{
printf("The year given is a leap year");
}
else
{
printf("The year given is not a leap year");
}
}
else
{
printf("This year given is a leap year");
}
}
else
{
printf("This year is not a leap year");
}
}

Output:

Output

Another set of output can be as below:

Output

Conclusion

So, this is how we can know whether any year is a leap year or not. We have our conditions of the divisibility test for 4, 100 and 400. We have actually analyzed and our coding is done using a single ‘if condition’ or multiple ‘if and else’ conditions. In either way, the same output can be obtained. In this way, we can get the leap year programming done in C programming language.

Recommended Articles

This is a guide to Leap Year Program in C. Here we discuss the introduction, Pseudocode/Algorithm, flowchart, and examples of leap year program in c. You may also have a look at the following articles to learn more –

  1. Factorial Program in C++
  2. Address Operator in C
  3. Leap Year Program in Java
  4. Leap Year Program in Python

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More