|
 |
|
 |
09-27-2005, 09:45 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 2
|
how to get leap years between years
hi im bernie,, any one who know the code of getting the leap years between two inputted years??.its hard for me to get the code so help me please...
this is the sample output,,
sample output1(past to recent)
Enter Year From: 1993
Enter Year To : 2000
Leap years are :
1996
2000
sample output2(recent to past)
Enter Year From: 2003
Enter Year To : 1985
Leap years are :
2000
1996
1992
please help me!!..thank you very much!!!
|
|
|
09-28-2005, 01:34 AM
|
#2 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
I think it would be easiest if you just wnet through each year with a for loop and evaluated it according to the predicates for a leap year.
-a year divisible by 4 is a leap year
-a year divisible by 100 is not a leap year
-a year divisble by 1000 is a leap year
-anything else is not a leap year
So 1996, 2000, and 2004 are but 2100 is not
__________________
Stop intellectual property from infringing on me
|
|
|
09-28-2005, 02:59 AM
|
#3 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
|
Something like:
Code:
#include <stdio.h>
#include <stdlib.h>
#define LENGTH 6
int leap_year(int* year);
int main(){
char buffer[LENGTH +1];
int year_from, year_to;
unsigned short int add = 1;
printf("Enter year From: ");
fflush(stdout);
if(!fgets(buffer, LENGTH, stdin))
{
printf("Error reading from stdin\n");
return -1;
}
year_from = strtol(buffer,(char **)NULL, 10);
if(year_from < 0)
{
printf("Error getting year from\n");
return -1;
}
printf("Enter year To: ");
fflush(stdout);
if(!fgets(buffer, LENGTH, stdin))
{
printf("Error reading from stdin\n");
return -1;
}
year_to = strtol(buffer,(char **)NULL, 10);
if(year_to <= year_from)
{
add = 0;
}
while(1)
{
if(add)
{
if(year_from > year_to)
break;
}
else
if(year_to > year_from)
break;
if(leap_year(&year_from))
printf("%d\n", year_from);
if(add)
++year_from;
else
year_from--;
}
return 0;
}
int leap_year(int* year)
{
if(!(*year%400))
return 1;
if(!(*year%100))
return 0;
if(!(*year%4))
return 1;
return 0;
}
Last edited by redhead; 09-28-2005 at 02:57 PM.
|
|
|
09-28-2005, 04:06 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 2
|
leap years
recent to past should be also included and will be printed...
like
Enter year From: 2005
Enter year to: 1985
then the leap years...thank you so much..your a genius!!
bernie
|
|
|
09-28-2005, 05:48 AM
|
#5 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
|
I altered my example to include that aswell..
But since I take it you only got a look at my first version, where it was only accepting past to recent you can figure out a way to inplement it in a less crude way than I did once I realized it was supposed to do that aswell.
|
|
|
09-28-2005, 12:12 PM
|
#6 (permalink)
|
|
[code][/code] enforcer
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
|
__________________
|
|
|
09-28-2005, 01:29 PM
|
#7 (permalink)
|
|
Jack of all trades
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
|
Oh dear how embarrassing, Valmont has shown that my 1000 year predicate should be 400 years.
__________________
Stop intellectual property from infringing on me
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 07:07 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|