473,386 Members | 1,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Leap year C shell script

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
4 9021
oberon
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
3,112 Expert 2GB
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
if ((( $year % 4 == 0) && ( $year % 100 != 0)) || $year % 400 == 0 )
echo "Leap Year";
else
echo "Not a leap year";
Apr 8 '08 #4
Laharl
849 Expert 512MB
We don't spoonfeed code here. Read these Posting Guidelines and you'll understand why.
Apr 8 '08 #5

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

Similar topics

22
by: deepak.rathore | last post by:
can someone give the regular expr. to validate leap yr like 02/29/2000,02/29/00 Thanks
2
by: owz | last post by:
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy . I seem 2 have gotten it 2 validate the year...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
8
by: SONIQ | last post by:
You can determine whether a year is a leap year by testing if it is divisible by 4. However, years that are also divisible by 100 are not leap years, unless they are also divisible by 400, in which...
12
by: Brigitte Behrmann | last post by:
Can anyone assist with my code. I just cannot get this to work: I have to write a script that allows the user to enter a year & then determine whether it is a leap year or not. The request is for a...
2
by: cybermom | last post by:
Hi another newbie here I am trying to get the message: The you entered is a leap year, but it does not happen, what did I do wrong with this script? <html> <HEAD> <SCRIPT...
15
by: ballygowanboy | last post by:
right, i've to write a small script to check if a year is leapyear or not. i understand that a) the year or number when divided by 400, 100 and 4, should leave me with a module remainder of 0,...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
5
by: jimix | last post by:
here's what i have. using microsoft studio #define _CRT_SECURE_NO_DEPRECATE #include <stdio.h> int main( void ) { int year, b, c, e; b = 4; c = 100;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.