Shell Script to Find A Number is Even or Odd
In this shell script guide, we are going to:
-
solve how to write code to check a number whether even or odd in shell script.
SHELL SCRIPT ODD EVEN STEPS:
Lets first learn the important steps needed for even odd program in shell script:
-
'clear' command to clear the screen.
-
Read a number which will be given by user.
-
Use `expr $n % 2`. If it equal to 0 then it is even otherwise it is odd.
-
Use if-else statements to make this program more easier.
-
Must include 'fi' after written 'if-else' statements.
EVEN OR ODD IN SHELL SCRIPT:
Given below is the steps check a number whether even or odd in shell script:
# HOW TO FIND A NUMBER IS EVEN OR ODD IN SHELL SCRIPT
# WRITTEN BY SURAJ MAITY
# TUTORIALSINHAND.COM
clear
echo "---- EVEN OR ODD IN SHELL SCRIPT -----"
echo -n "Enter a number:"
read n
echo -n "RESULT: "
if [ `expr $n % 2` == 0 ]
then
echo "$n is even"
else
echo "$n is Odd"
fi
WRITTEN IN CYGWIN:
Shell script to find a number is even or odd:
OUTPUT: Below is the output for even odd program in shell script
---- EVEN OR ODD IN SHELL SCRIPT -----
Enter a number:23
RESULT: 23 is Odd
If you face any problem regarding writing code or running problem, just follow these steps ->
RUN PROGRAM IN SHELL SCRIPT
EXPLANATION:
Given below is further explanation with an example on shell program to check even or odd.
Here we have explained one example for each odd and even. Lets see the example below.
-----------------------------
FOR EVEN NUMBER
-----------------------------
Suppose, user enter 10.
if( n%2 ==0 ) then it will print even number because of we divide 10 by 2 then remainder must be 0.
i.e (10 %2 == 0), so, it is even number
----------------------------
FOR ODD NUMBER
----------------------------
Suppose, user enter 13.
if( n%2 !=0 ) then it will print odd number because of we divide 13 by 2 then remainder must not be 0.
i.e (13 %2 != 0), so, it is odd number.
You can visit my video tutorial where I have explained the concept for shell script to find a number is even or odd:
This wraps up our session on shell script to find a number is even or odd or even or odd in shell script or shell program for odd or even.
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Page Views :
Published Date :
May 08,2024