C & C++ teachers in a lot of schools use leap year calculation program as a classic example to demonstrate the nested if-else conditional scenarios of these languages.
The algorithm or logic for Leap year is basically that:
1. A year is a leap year if it is divisible by 4 but not by 100.
2. If a year is divisible by both 4 and by 100, then it can only be a leap year if it is also divisible by 400.
A simple C / C++ programming code that uses this logic for determining whether a year is a Leap year or not is given below:
#include<stdio.h>
The algorithm or logic for Leap year is basically that:
1. A year is a leap year if it is divisible by 4 but not by 100.
2. If a year is divisible by both 4 and by 100, then it can only be a leap year if it is also divisible by 400.
- Going by the above logic, any year that is divisible by 400 must definitely be a leap year (because any number divisible by 400, is also divisible by 4)
- If a number is not divisible by 400, then we can check whether it is divisible by 100. If that is the case then it is definitely not a leap year (even if it is divisible by 4. this is as per condition no.1 above)
- After checking divisibility of the year by 400, and 100, lastly we can check whether the number is exactly divisible by 4. If it is, then its a leap year, else its not a leap year.
#include<stdio.h>
#include<conio.h>
void main() {
int year;
clrscr();
printf("Enter the Year that you want to check : ");
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 not a Leap Year", year);
printf("\nPress any key to Quit...");
getch();
}
mine's a little different.
ReplyDelete(consider "x" as a variable for year.)
#include
#include
main()
{
int x;
printf("Enter a year: ");
scanf("%d",&x);
for(x=x;x>0;x=x-4)
{
}
if(x==0)
{
printf("\nIt's a leap year!");
}
else
{
printf("\nIt's not a leap year!");
}
getch();
}
really nice solution.....
DeleteThe complexity is high here and will increase as the x increases.
Deletecomplexity is O(X/4).... whereas the above the complexity is O(1).
simlest program to cheak a leap year :)
ReplyDelete#include
#include
main()
{
int year;
printf("enter a year");
scanf("%d",&year);
if (year%100==0)
{
if (year%400==0)
printf("it is a leap year");
else
printf("it is a not leap year");
}
else
{
if (year%4==0)
printf("it is a leap year");
else
printf("it is a not leap year");
}
getch();
}
nice solutions. thnks guys
ReplyDeletei guess i've got a shortter and better solution for this...so it is something like this
ReplyDelete#include
int main()
{
int year;
printf("enter the year");
scanf("%d",&year);
if(year%400==0||year54==0&&year%100!=0)
printf("it is a leap year");
else
printf("it is not a leap year");
return 0;
}
int main()
ReplyDelete{
int year;
printf("enter the year");
scanf("%d",&year);
if(year%4==0)
printf("it is a leap year");
else
printf("it is not a leap year");
return 0;
}
...............................................................
is that code right ???
please explain ....
yeah.
Deleteyour answer is right.
Because, if you divide by 4 and there is no remaining then that is leap year. hope its simple to understand the logic. :)
A leap year is every 4 years, but not every 100 years, then again every 400 years.
DeleteIf logic is %4==0
Then it failed in the case of 1900 which is not a leap year but which is divided by 4
i guess the simplest one is........
ReplyDelete#include
#include
void main()
{
int a;
clrscr();
printf("Enter the year:");
scanf("%d"&a);
if(a%4==0)
{
if((a%100==0 && a%100!=0))
printf("The %d year is NOT a LEAP year");
else
printf("The %d year is a LEAP year");
}
else
{
printf("The %d year is NOT a LEAP year");
}
getch();
}
There will be an error in " scanf("%d"&a); " this line and other others lines also.
Deleteimprove yourself :)
1900 is not a leap year so 1904 also not a leap year both are div by 4
ReplyDelete2000 and 2004 is a leap year any of these programs not satisfying these conditions
1904 is a leap year
DeleteA leap year is something which comes for every years
But not divisible by 100
But divisible by 400
This comment has been removed by the author.
ReplyDelete#include
ReplyDeleteint main()
{
int n;
printf("enter the year:-");
scanf("%d",&n);
if(n>0)
{
printf("valid input\n");
if(n%4==0&&n%100!=0||n%400==0)
{
printf("this is leap year\n");
}
else
{
printf("this is not leap yaer\n");
}
}
else
{
printf("invalid input\n");
}
}
"C program to check leap year"
ReplyDeleteQuestion:
How do i tweak the output of this code:
~let say i type in 2012 and the output should be February 29, 2014 Wednesday is a leap year (same thing with the other leap year)
~ i type in 2013 and the output should be 2013 is not a leap year.
Thanks in advance.
check for the year 2700,2500 these are exceptional cases,and also check calendar of these years pls thank you
ReplyDeletewhy to use 100 to check leap year in c?
ReplyDeleteThe leap year will not repeat for 100 years but it will repeat for every 400 years
Delete//----- This is perhaps simplest of all:
ReplyDeleteint set_max_days_for_Feb(int year)
{if (year & 3 )return 28; // year within century
if (year % 100 )return 29; // leap centuries: 1800,1900,2100
if (year % 400 )return 28; // leap centuries (1600,2000,2400,...)
return 29;
}
This comment has been removed by the author.
ReplyDelete#include
ReplyDeleteint main ()
{
int year;
scanf ("%d", &year);
if (((year % 4) == 0 && (year % 100) != 0) || (year % 400) == 0) {
printf ("The given year is a leap year\n");
}
else {
printf ("The given year is not a leap year\n");
}
return 0;
}
why use 100?
ReplyDeletewhy use 100?
ReplyDeleteThis comment has been removed by the author.
ReplyDelete#include
ReplyDeleteusing namespace std;
int main()
{
int year,div;
cout<<"input an year:";
cin>>year;
if(year%100==0)
div=year%400;
else
div=year%4;
if(div==0)
cout<<"Leap Year..";
else
cout<<"not a leap year";
return 0;
}
why use 100 and 400 in program
ReplyDelete