I made a program using C to find whether the entered year is a leap year or not. But unfortunately its not working well. It says a year is leap and the preceding year is not leap.
#include<stdio.h>
#include<conio.h>
int yearr(int year);
void main(void)
{
int year;
printf("Enter a year:");
scanf("%d",&year);
if(!yearr(year))
{
printf("It is a leap year.");
}
else
{
printf("It is not a leap year");
}
getch();
}
int yearr(int year)
{
if((year%4==0)&&(year/4!=0))
return 1;
else
return 0;
}
After reading the comments i edited my coding as:
#include<stdio.h>
#include<conio.h>
int yearr(int year);
void main(void)
{
int year;
printf("Enter a year:");
scanf("%d",&year);
if(!yearr(year))
{
printf("It is a leap year.");
}
else
{
printf("It is not a leap year");
}
getch();
}
int yearr(int year)
{
if((year%4==0)
{
if(year%400==0)
return 1;
if(year%100==0)
return 0;
}
else
return 0;
}
As other have also mentioned condition for leap year is not correct. It should:
Read it here how to check leap year in C.
Most efficient leap year test:
This code is valid in C, C++, C#, Java, and many other C-like languages. The code utilizes a single TRUE/FALSE expression that consists of three separate tests:
year & 3
year % 25
year & 15
A complete discussion of how this code works appears below, but first a discussion of Wikipedia's algorithm is called for:
Wikipedia algorithm is INEFFICIENT/UNRELIABLE
Wikipedia has published a pseudo-code algorithm (See: Wikipedia: Leap year - Algorithm) that has been subjected to constant editing, opinion, and vandalism.
DO NOT IMPLEMENT WIKIPEDIA ALGORITHM!
One of the longest-standing (and inefficient) Wikipedia algorithms appeared as follows:
The above algorithm is inefficient because it always performs the tests for the 400th year and 100th year even for years that would quickly fail the "4th year test" (the modulo 4 test)—which is 75% of the time! By re-ordering the algorithm to perform the 4th year test first we speed things up significantly.
"MOST-EFFICIENT" PSEUDO-CODE ALGORITHM
I provided the following algorithm to Wikipedia (more than once):
This "most-efficient" pseudo-code simply changes the order of tests so the division by 4 takes place first, followed by the less-frequently occurring tests. Because "year" does not divide by four 75-percent of the time, the algorithm ends after only one test in three out of four cases.
DISCUSSION OF "MOST-EFFICIENT" LEAP YEAR TEST
Bitwise-AND in place of modulo:
I have replaced two of the modulo operations in the Wikipedia algorithm with bitwise-AND operations. Why and how?
Performing a modulo calculation requires division. One doesn't often think twice about this when programming a PC, but when programming 8-bit microcontrollers embedded in small devices you may find that a divide function cannot be natively performed by the CPU. On such CPUs, division is an arduous process involving repetitive looping, bit shifting, and add/subtract operations that is very slow. It is very desirable to avoid.
It turns out that the modulo of powers of two can be alternately achieved using a bitwise-AND operation (see: Wikipedia: Modulo operation - Performance Issues):
x % 2^n == x & (2^n - 1)
Many optimizing compilers will convert such modulo operations to bitwise-AND for you, but less advanced compilers for smaller and less popular CPUs may not. Bitwise-AND is a single instruction on every CPU.
By replacing the
modulo 4
andmodulo 400
tests with& 3
and& 15
(see below: 'Factoring to reduce math') we can ensure that the fastest code results without using a much slower divide operation.There exists no power of two that equals 100. Thus, we are forced to continue to use the modulo operation for the 100th year test, however 100 is replaced by 25 (see below).
Factoring to simplify the math:
In addition to using bitwise-AND to replace modulo operations, you may note two additional disputes between the Wikipedia algorithm and the optimized expression:
modulo 100
is replaced bymodulo 25
modulo 400
is replaced by& 15
The 100th year test utilizes
modulo 25
instead ofmodulo 100
. We can do this because 100 factors out to 2 x 2 x 5 x 5. Because the 4th year test already checks for factors of 4 we can eliminate that factor from 100, leaving 25. This optimization is probably insignificant to nearly every CPU implementation (as both 100 and 25 fit in 8-bits).The 400th year test utilizes
& 15
which is equivalent tomodulo 16
. Again, we can do this because 400 factors out to 2 x 2 x 2 x 2 x 5 x 5. We can eliminate the factor of 25 which is tested by the 100th year test, leaving 16. We cannot further reduce 16 because 8 is a factor of 200, so removing any more factors would produce a unwanted positive for a 200th year.The 400th year optimization is greatly important to 8-bit CPUs, first, because it avoids division; but, more important, because the value 400 is a 9-bit number which is much more difficult to deal with in an 8-bit CPU.
Short-circuit Logical AND/OR operators:
The final, and most important, optimization used are the short-circuit logical AND ('&&') and OR ('||') operators (see: Wikipedia: Short-circuit evaluation), which are implemented in most C-like languages. Short-circuit operators are so named because they do not bother to evaluate the expression on the right side if the expression on the left side, by itself, dictates the outcome of the operation.
For example: If the year is 2003, then
year & 3 == 0
is false. There is no way that the tests on the right side of the logical AND can make the outcome true, so nothing else gets evaluated.By performing the 4th year test first, only the 4th year test (a simple bitwise-AND) is evaluated three-quarters (75 percent) of the time. This speeds up program execution greatly, especially since it avoids the division necessary for the 100th year test (the modulo 25 operation).
NOTE ON PARENTHESES PLACEMENT
One commenter felt parentheses were misplaced in my code and suggested the sub-expressions be regrouped around the logical AND operator (instead of around the logical OR), as follows:
The above is incorrect. The logical AND operator has higher precedence than logical OR and will be evaluated first with or without the new parentheses. Parentheses around the logical AND arguments has no effect. This might lead one to eliminate the sub-groupings entirely:
But, in both cases above, the right side of the logical OR (the 400th year test) is evaluated almost every time (i.e., years not divisible by 4 and 100). Thus, a useful optimization has been mistakenly eliminated.
The parentheses in my original code implement the most optimized solution:
Here, the logical OR is only evaluated for years divisible by 4 (because of the short-circuit AND). The right side of the logical OR is only evaluated for years divisible by 4 and 100 (because of the short-circuit OR).
NOTE FOR C/C++ PROGRAMMERS
C/C++ programmers might feel this expression is more optimized:
This is not more optimized! While the explicit
== 0
and!= 0
tests are removed, they become implicit and are still performed. Worse, the code is no longer valid in strongly-typed languages like C# whereyear & 3
evaluates to anint
, but the logical AND (&&
), OR (||
) and NOT (!
) operators requirebool
arguments.Change it like above. Also read this.
Calculate max/last day for month: 1..12, year: 1..3999
Your logic to determine a leap year is wrong. This should get you started (from Wikipedia):
x modulo y
means the remainder ofx
divided byy
. For example, 12 modulo 5 is 2.