07 October 2011

Finding Prime Number using Shell Script in Linux/Unix

Leave a Comment

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


finding prime number in bash


If You Liked This Post Please Take a Time To Share This Post

You May Also Like...

0 comments:

Post a Comment