Tuesday, January 24, 2023

Unix Shell Programming Example for B.Sc, B.Tech, BCA, MCA

 4.Check a number whether it is odd positive,odd negative,even positive,even negative.

PROGRAM:

echo "enter a number:"
read n
if [ $n -ge 0 ]
then
    if [ `expr $n % 2` -eq 0 ]
    then
        echo "$n is a even positive number"
    else
        echo "$n is a odd positive number"
    fi
else
    if [ `expr $n % 2` -eq 0 ]
    then
        echo "$n is a even negative number"
    else
        echo "$n is a odd negative number"
    fi
fi


OUTPUT:


[bgc@localhost ~]$ sh 5.sh
enter a number:
3
3 is a odd positive number
[bgc@localhost ~]$ sh 5.sh
enter a number:
0
0 is a even positive number
[bgc@localhost ~]$ -9
bash: -9: command not found...
[bgc@localhost ~]$ sh 5.sh
enter a number:
-9
-9 is a odd negative number
[bgc@localhost ~]$ sh 5.sh
enter a number:
-10
-10 is a even negative number
[bgc@localhost ~]$

No comments:

Post a Comment