By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

Leap year C shell script

P: 5
Hello. So im a little bit stuck on one of my homework questions.
heres the question:

Write a C Shell Script that performs the following functions. Asks the user to input a year. The script should then calculate whether or not the year is a leap year. The script should caculate whether or not it is a leap year using the following algorithm. A leap year is defined as any year divisible by 4 but not by 100. However, if a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400. The script should implement the math to produce this algoritm and tell the user whether or not the year entered is a leap year.

heres what i have so far:
************************************************** ***********
#!/bin/csh

echo Please input a year
set year = $<

if ( $year / 4 ) then
echo this is a leap year
else
echo this is not a leap year
endif

************************************************** **********

i know its not exactly what the teacher wanted but i was just messing around with it to see if this could tell me whether or not it was a leap year and for every year i input it tells me its a leap year. i dont know what im doing wrong. any help would be helpful thanks
Feb 29 '08 #1
Share this Question
Share on Google+
4 Replies

P: 14
The problem here is the expression if ( $year / 4 ). This test will return true as long as it is a valid division, which in this case is true for all values of $year. What you want is a test that returns true when the division does not give any remainder. You can use the modulo operator for this:
Expand|Select|Wrap|Line Numbers
  1. if ( $year % 4 == 0)
Mar 30 '08 #2

Nepomuk
Expert 2.5K+
P: 3,112
Also, your algorithm isn't quite correct - check http://en.wikipedia.org/wiki/Leap_year#Algorithm for a correct algorithm in pseudocode.

Greetings,
Nepomuk
Apr 8 '08 #3

P: 1
if ((( $year % 4 == 0) && ( $year % 100 != 0)) || $year % 400 == 0 )
echo "Leap Year";
else
echo "Not a leap year";
Apr 8 '08 #4

Expert 100+
P: 849
We don't spoonfeed code here. Read these Posting Guidelines and you'll understand why.
Apr 8 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.