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

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

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

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

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

Ask Your Question

Weekly Poll

Did you watch the X-Files when it was on TV?

Discuss in The Lounge

Poll History