Saturday, February 22, 2014

UNIX Shell Scripting Leap Year

I found the following algorithm on Leap Years.

In your favorite editor (like gedit), type in the following code.

Code: (leap_year.sh)
----------------------
echo "Enter a year : "
read y

if [ $(( $y % 4 )) -eq 0 -a $(( $y % 100 )) -ne 0 -o $(( $y % 400 )) -eq 0 ]
then
echo "$y is a leap year"
else
echo "$y is not a leap year"
fi

Execution:
----------
1. Open Terminal
2. Navigate to the directory containing the above script
3. bash leap_year.sh
or you can also do  ./leap_year.sh provided you got execute permissions for the script.

Happy Coding.

No comments:

Post a Comment