Navigation Menu

Currently the navigation bar is not functioning. Use the Blog Archive or the Google Custom Search to find content.

Friday 14 June 2013

Shell script to display prime numbers

Bash script to display prime numbers.

Links that you may find important :

https://en.wikipedia.org/wiki/Prime_number
http://en.wikipedia.org/wiki/List_of_prime_numbers
http://en.wikipedia.org/wiki/2_%28number%29

Code :
echo 'Enter no'
read x
n=2
while [ $n -le $x ]
do
i=2
count=1

while [ $i -lt $n ]
do
if [ `expr $n % $i` -eq 0 ]
then
count=0
break
fi
i=`expr $i + 1`
done

if [ $count -eq 1 ]
then
echo "$n is Prime"
fi

n=`expr $n + 1`
done
Output :
2 is Prime
3 is Prime
5 is Prime
7 is Prime
Note : Leave a comment if you feel the program is incorrect and/or has errors and/or if the program and its output don't match. Please report about broken links.

1 comment: