Tuesday, January 24, 2023

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

 7. Prime number checking.

PROGRAM:

echo "enter a number:"
read n

    c=0
    d=1
    while [ $d -le $n ]
    do
        if [ `expr $n \% $d` -eq 0 ]
        then
            c=`expr $c + 1`
        fi
        d=`expr $d + 1`
    done

if [ $c -eq 2 ]
then
    echo "prime number"
else
    echo "not a prime number"   
fi


OUTPUT:

[bgc@localhost ~]$ sh 8.sh
enter a number:
2
prime number
[bgc@localhost ~]$ sh 8.sh
enter a number:
1
not a prime number
[bgc@localhost ~]$ sh 8.sh
enter a number:
0
not a prime number
[bgc@localhost ~]$ sh 8.sh
enter a number:
5
prime number
[bgc@localhost ~]$ sh 8.sh
enter a number:
4
not a prime number
[bgc@localhost ~]$

No comments:

Post a Comment