LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2004, 10:02 PM   #1
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Rep: Reputation: 15
I don't know how to solve a problem in C


Hi I'm working through the programming exercises in my Practical C Programming 3rd Ed, and I'm having trouble even creating a formula to solve the problem. The problem is as follows:

A leap year is any year divisible by 4, unless the year is divisible by 100, but not 400. Write a program to tell if a year is a leap year.

Now at first I thought by possibly using the if() statement I could figure this out and the if() statement still might suffice, but it appears I need to tell whether the condition is positive or false aka whether the year is divisble by 100 but not 400. I just don't know how to do that and I was wondering if someone could either show me how or point me in the right direction. Any help would be greatly appreciated. Thanks again to all the cool folks who've helped me in the past.

-Spiros Vondas
 
Old 07-19-2004, 10:15 PM   #2
rgiggs
Member
 
Registered: Apr 2004
Location: berkeley, ca
Distribution: slk10, winxp
Posts: 313

Rep: Reputation: 30
how about this?

Code:
  if (divisible by 4)
    if (not divisible by 100)
      leap
    else
      if (divisible by 400)
        leap
      else
        non leap
  else
    non leap;

Last edited by rgiggs; 07-19-2004 at 10:20 PM.
 
Old 07-19-2004, 10:39 PM   #3
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Original Poster
Rep: Reputation: 15
Thanks....

Does leap need anything after it or need meet any conditions? I've never seen ti used before and am having trouble find out information on it.
-Spiros
 
Old 07-19-2004, 11:20 PM   #4
osvaldomarques
Member
 
Registered: Jul 2004
Location: Rio de Janeiro - Brazil
Distribution: Conectiva 10 - Conectiva 8 - Slackware 9 - starting with LFS
Posts: 519

Rep: Reputation: 34
For you to check this divisibility all you need is check for remainder == 0; So, instead of "/" you need to use "%"
Code:
if (given_year % 4 == 0 && (given_year % 100 != 0 || given_year % 400 == 0)) // leap year
 
Old 07-20-2004, 12:56 AM   #5
nostrum
Member
 
Registered: Jun 2004
Location: New York
Distribution: Red Hat
Posts: 32

Original Poster
Rep: Reputation: 15
WOOHOO!

Thank you so much that's exactly what I needed I appreciate it!
-Spiros Vondas
 
Old 07-20-2004, 07:57 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Short one, just for fun:
Code:
if ((!(y%4)&&y%100)||!(y%400)) {
    /* y is leap */
}
<edit>
Hmm, after reading the thread a second time, mine is not that much shorter actually (apart from the var-name for the year)
</edit>

Last edited by Hko; 07-20-2004 at 08:15 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help to solve this problem TheTiger Fedora 1 10-26-2005 01:01 PM
help..how i can solve this problem?// TAAN Linux - General 2 02-16-2005 07:53 AM
Who can help me to solve this problem? Annie0716 Programming 2 08-09-2004 07:59 PM
No one can solve my problem Sundance Linux - Newbie 5 12-08-2003 01:13 PM
The only problem I couldn't solve... Bager Linux - Laptop and Netbook 2 07-21-2003 08:37 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration