Tuesday, January 12, 2016

Shell program to find out whether the given number is prime or not.

# shellprime.sh
# Program :         To find out whether the given number is prime or not.
# Author :             sujith
# Date :              16/03/2015

clear
echo "Enter the number"
read n
flag=0
i=2
if test $n -lt 1
 then
 flag=1
fi

 while test $i -lt $n
        do
        r=$(echo "$n%$i" | bc)

         if test $r -eq 0
         then
           flag=1
         fi
        i=`expr $i + 1`
        done

if test $flag -eq 0
then
echo "It is a prime number"
else
echo "It is not  a prime number"
fi


Output:

[141740@localhost ~]$ sh shellprime.sh
Enter the number
2
It is a prime number
[141740@localhost ~]$

Enter the number
8
It is not  a prime number
[141740@localhost ~]$




No comments:

Post a Comment