Shell Script to Check Leap Year
In this shell script program, we will learn to write:
-
shell script to check leap year or not
To solve leap year in shell script more easily, you have to know some basic concepts like:
-
Clear command
-
Echo command
-
Taking input from user.
-
If-else statements
-
Module operation
LEAP YEAR RULES:
Every four years, a leap year come once. Per year contain 365.24 days but to correct and keep track of the solar calendar, we introduced leap year concept. Unlike regular years( 365 days), leap year has 366 days.
-
A year will be leap year if it is divided by 4 and if it’s remainder is equal to 0.
-
If above result is equal to 0, then we will consider that given year by the user is a leap year otherwise it is not.
ALGORITHM TO CHECK LEAP YEAR:
-
STEP 1: START SHELL SCRIPT
-
STEP 2: CLEAR THE SCREEN.
-
STEP 3: TAKE A YEAR INPUT BY THE USER.
-
STEP 4: IMPLEMENT IF-ELSE STATEMENT.
-
STEP 5: GIVE CONDITION #YEAR % 4
-
STEP 6: IF RESULT ==0 THEN IT IS LEAP YEAR OTHERWISER IT IS NOT
-
STEP 7: STOP SHELL SCRIPT
Shell script to check leap year or not
Given below is a shell script for checking leap year:
clear
echo "LEAP YEAR SHELL SCRIPT"
echo -n "Enter a year:"
read year_checker
if [ `expr $year_checker % 4` -eq 0 ]
then
echo "$year_checker is a leap year"
else
echo "$year_checker is not a leap year"
fi
CODE IMAGE:
OUTPUT:
LEAP YEAR SHELL SCRIPT
Enter a year:2020
2020 is a leap year
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Page Views :
Published Date :
Jan 23,2021