Friday, October 7, 2011
9:49 PM

Finding Prime Number using Shell Script in Linux/Unix


Q. How do I find prime number using Shell Script
Ans:

#!/bin/bash
prime_1=0
echo "enter the range"
read n
echo " Primenumber between 1 to $n is:"
echo "1"
echo "2"
for((i=3;i<=n;))
do
for((j=i-1;j>=2;))
do
if [ `expr $i % $j` -ne 0 ] ; then
prime_1=1
else
prime_1=0
break
fi
j=`expr $j - 1`
done
if [ $prime_1 -eq 1 ] ; then
echo $i
fi
i=`expr $i + 1`
done


Output

0 comments:

Post a Comment