Articles

Print even series in shell script up to n terms

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

CODE IMAGE:

shell script to print even series from 1 to nth number

 

OUTPUT:

how to print even series up to nth number in shell script

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.

 

 


Shell Script Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Suraj Maity
Page Views :    Published Date : Feb 01,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!