Print even series in shell script up to n terms
In this shell script program, we will learn to:
-
print even series in shell script up to n terms. The number will be given by the user.
Lets see algorithm, shell program and snapshot for how to print even series up to nth number in shell script.
ALGORITHM TO PRINT EVEN SERIES:
Given below is shell script to print even series from 1 to nth number:
STEP 1: START
STEP 2: DCLARE A VARIABLE (Suppose $checker) AND JUST INITIALIZE WITH 0.
STEP 3: READ NUMBER ( SUPPOSE num) FROM THE USER.
STEP 4: RUN WHILE LOOP UPTO $num
STEP 5: GIVE WHILE LOOP CONDITION -> $checker –le $num
STEP 6: DIVIDE $checker BY 2 AND IF IT REMAINDER EQUAL TO ZERO THEN IT IS EVEN AND JUST PRINT IT
STEP 7: OTHERWISE INCREMENT $checker = $checker + 1
STEP 8: PRINT EVEN SERIES UPTO $num
STEP 9: STOP
CODE TO PRINT EVEN SERIES IN SHELL:
Given below is shell script to print even series from 1 to nth number:
#Print up to nth number of even series in shell script
clear
echo "------EVEN SERIES------"
echo -n "Enter a number: "
checker=0
read num
while test $checker -le $num
do
ii=`expr $checker % 2`
if test $ii -eq 0
then
echo "$checker"
fi
checker=`expr $checker + 1`
done
OUTPUT:
EXPLANATION:
-
‘if test $ii -eq 0’is used to check from 0 to nth numbers whether they are even or odd.
-
If above condition is true then it is even and we should print it.
-
Otherwise ‘$checker=$checker+1’ will be performed to check next digits.
-
We have to remember that up to highest number (which will be given by the user) we have to perform loop operation.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Page Views :
Published Date :
Feb 01,2021