Monday, February 26, 2024
2
rated 0 times [  2] [ 0]  / answers: 1 / hits: 631  / 1 Year ago, wed, february 1, 2023, 2:58:16

The following program is giving error on line 7 and 9: the `while` loop does not seem to be working. It is a program to show all the prime and composite numbers from 3 to a desired given input.

``````echo prograam to find all the prime numbers from 3 to given number
echo enter the last number
n=\$number
i=3
m=3
while [ \$m -le \$n ]
do
while [ \$i -le \$m ]
do
m=`expr \$n % \$i`
i=`expr \$i +1`
if [ \$m -eq 0 ]
then
echo \$n is a prime number

else
echo \$n is not a prime number

fi
done
m=`expr \$m +1`
done
``````

More From » scripts

0

Re-wrote the shell script for you:

``````echo "program to find all the prime numbers from 3 to given number"
echo "enter the last number"
n=\$number
i=2
m=2
is_prime=0
while [ "\$m" -lt "\$n" ]
do
while [ "\$i" -le "\$m" ]
do
m=\$(expr \$n % \$i)
i=\$(expr \$i + 1)
if [ "\$m" -eq 0 ]
then
echo "\$n is not a prime number"
exit
else
is_prime=1
fi
done
m=\$(expr "\$m" + 1)
done
if [ "\$is_prime" -eq 1 ]; then
echo "\$n is a prime number"
fi
exit
``````

Referred to the following for this:

Total Points: 461
Total Questions: 126