Write a linux shell program to perform check whether a number is prime or not using while

Prime or not using while


Code:

clear 
echo --------------------------------------------- 
echo '\tPrime number or Not' 
echo --------------------------------------------- 
echo Enter the  number 
read n 
i=2 
ans=0 
while [ $i -le $(expr $n - 1) ] 
do 
        if [ $(expr $n % $i) -eq 0 ] 
        then 
                ans=$(expr $ans + $i) 
        fi 
        i=$(expr $i + 1) 
done 
if [ $ans -eq 0 ] 
then 
        echo The number $n is a prime. 
else 
        echo The number $n is not a prime. 
fi 

Output:

--------------------------------------------- 
Prime number or Not 
--------------------------------------------- 
Enter the number 
11 
The number 11 is a prime

No comments

Powered by Blogger.