Monday, 9 July 2012

Write a shell script to check whether a given number is prime or not.

echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
fi
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
fi

--------------------------------------------------------------------------------------------------------------
                OUTPUT
Enter a number:
5
The number is Prime

5 comments:

Vachan Nd said...

This doesn't hold good for num=9

Cindy Dy said...

Excellent and decent post. I found this much informative, as to what I was exactly searching for. Thanks for such post and keep it up.

Bry
www.gofastek.com

Sunny Prasad said...

perfect

ajmath shaik said...

Easiest Program to Find Whether Given Number Is Prime Number Or Not

Leslie Lim said...

First time I commented in a blog! I really enjoy it. You have an awesome post. Please do more articles like this. I'm gonna come back surely. God bless.

Rica
www.imarksweb.org

Post a Comment