Tuesday, 11 June 2013

shell script to accept a number from the user and check the number entered is prime or not.

Code:
echo "Enter the number"
read num
i=2
flag=0
while [ $i -lt $num ]
do
   k=`expr $num % $i`
   if  test $k -eq 0
   then
       flag=1
   fi
   i=`expr $i + 1`
done
if test $flag -eq 0
then
   echo "Number is prime"
else
   echo "Number is not prime"
fi    


Output:
Enter the number
2
Number is prime

Enter the number
4
Number is not prime

No comments:

Post a Comment