Monday, February 26, 2024
 Popular · Latest · Hot · Upcoming
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
read 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

 Answers
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"
read 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:




[#26978] Thursday, February 2, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rtbrbab

Total Points: 461
Total Questions: 126
Total Answers: 117

Location: Saudi Arabia
Member since Fri, Jul 1, 2022
2 Years ago
rtbrbab questions
Fri, May 5, 23, 14:22, 11 Months ago
Tue, Nov 16, 21, 14:41, 2 Years ago
Thu, Mar 23, 23, 20:47, 1 Year ago
Sat, Jun 18, 22, 21:36, 2 Years ago
;