Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 09-27-2005, 09:45 PM   #1 (permalink)
Bernie
Registered User
 
Join Date: Sep 2005
Posts: 2
Bernie is on a distinguished road
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!!!
Bernie is offline   Reply With Quote
Old 09-28-2005, 01:34 AM   #2 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
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
teknomage1 is offline   Reply With Quote
Old 09-28-2005, 02:59 AM   #3 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
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;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 09-28-2005 at 02:57 PM.
redhead is offline   Reply With Quote
Old 09-28-2005, 04:06 AM   #4 (permalink)
Bernie
Registered User
 
Join Date: Sep 2005
Posts: 2
Bernie is on a distinguished road
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
Bernie is offline   Reply With Quote
Old 09-28-2005, 05:48 AM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,709
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 09-28-2005, 12:12 PM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Urgently need help, Functions and leap year problem
__________________
Valmont is offline   Reply With Quote
Old 09-28-2005, 01:29 PM   #7 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
Oh dear how embarrassing, Valmont has shown that my 1000 year predicate should be 400 years.
__________________
Stop intellectual property from infringing on me
teknomage1 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
USA: Spam-king got 9 years in jail redhead Code Newbie News 2 04-09-2005 07:40 PM
Urgently need help, Functions and leap year problem cleverest Standard C, C++ 17 04-01-2005 12:29 PM
Happidely didely new years!! sde Lounge 4 01-05-2003 12:57 PM


All times are GMT -8. The time now is 07:07 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting