Articles

shell program to find prime no.s

July 30, 2010 at 01:18:01
Specs: Windows XP

How do you write a shell Program of printing prime numbers between 1 to 50?

See More: shell program to find prime no.s

Report •


#1
November 22, 2010 at 07:40:08

echo enter a number
read num
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo $num is not prime
echo since it is divisible by $i
exit
fi
i=`expr $i + 1`
done
echo $num is a prime number

Report •

#2
March 1, 2011 at 22:38:08

for this code if i give 1 as input means it will display 1 as prime number but 1 is neither prime nor composite

Report •

#3
March 1, 2011 at 22:42:28

The correct code is

echo enter a number
read num
if [ $num -eq 1 ]
then
echo "1 is neither prime nor composite"
else
i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo $num is not prime
exit
fi
i=`expr $i + 1`
done
echo $num is a prime number
fi


Report •

Related Solutions

#4
March 1, 2011 at 22:46:48

for this code if i give 1 as input means it will display 1 as prime number but 1 is neither prime nor composite

Report •


Ask Question