GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 09-Dec-2003, 21:37
lena lena is offline
New Member
 
Join Date: Dec 2003
Posts: 1
lena is an unknown quantity at this point

leap year


can anyone suggest a formula to use when calculating the next leap year after a user inputs a year which is not a leap year and you want to show the next leap year.

i can not use any loops in this formula.

thanks
  #2  
Old 06-Jan-2004, 23:27
AndrewL AndrewL is offline
New Member
 
Join Date: Jan 2004
Posts: 1
AndrewL is on a distinguished road
Note that leap years are equally divisible by 4. Hence there is no remainder. You'll also notice a sequence forming with the remainders when you divide other years by 4. For instance:

2000/4 = 500 remainder 0
2001/4 = 500 remainder 1
2002/4 = 500 remainder 2
2003/4 = 500 remainder 3
2004/4 = 501 remainder 0
2005/4 = 501 remainder 1
2006/4 = 501 remainder 2
2007/4 = 501 remainder 3
The remainder sequence repeats itself.

You can then use this recurring sequence within an if/else structure and use the modulus operator to create the condition.

For example:
CPP / C++ / C Code:
if (year % 4 == 1) 
   year = year + 3;
   cout << year;

else if (year % 4 == 2)
   year = year + 2;
   cout << year;

else if (year % 4 == 3)
   year = year + 1;
   cout << year;

else 
    cout << year << "is a leap year"; 

Apologies for the syntax it's been a while since I've touched C++ or even did any programming.

Hope this helps :-)
Andrew Lai

Quote:
Originally Posted by lena
can anyone suggest a formula to use when calculating the next leap year after a user inputs a year which is not a leap year and you want to show the next leap year.

i can not use any loops in this formula.

thanks
 
 

Recent GIDBlogDeveloping GUIs with wxPython (Part 1) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking for Email Hosting, price per year. JdS Member Announcements, Advertisements & Offers 12 29-Feb-2004 20:44
Quote of the Year BobbyDouglas Open Discussion Forum 2 14-Sep-2003 13:10
Unlimited storage space/unlimited bandwidth with free domain only $99 per year whssn Web Hosting Advertisements & Offers 2 09-Sep-2003 03:46
1st msg of year - Check it out jrobbio Websites Reviewed Forum 9 15-Feb-2003 19:21
Happy New Year nniehoff Open Discussion Forum 0 02-Jan-2002 15:24

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 12:24.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.