Skip to main content

Program to check whether a number is prime or not in Linux/Unix

echo "Enter a number"
read n
i=1
c=0
while [ $i -le $n ]
do
if [ ` expr $n \% $i ` -eq 0 ]
then
c=` expr $c + 1 `
fi
i=` expr $i + 1 `
done

if [ $c -eq 2 ]
then
echo "Prime"
else
echo "Not a prime"
fi

Comments

Popular posts from this blog

unix commands