Friday, 12 December 2014

Shell Scripts to check for Prime numbers

Write a Shell Script to check whether a given number is Prime or not

Solution:
echo Enter any number
read num
b=`expr $num / 2`
if test $b -eq 1
then
echo It is a Prime number
fi
n=2
x=0
while [ $b -ge $n ]
do
c=`expr $num % $n`
if test $c -eq 0
then
echo Number is not prime
x=0
break
else
x=1
fi
n=`expr $n + 1`
done
if test $x -eq 1
then
echo It is a Prime number
fi

Solution:

No comments:

Post a Comment