Monday, 24 October 2016

script to find prime number

#!/bin/bash
echo -n "Enter a number: "
readnum
expr $num + 1  &> /dev/null

if [ $? -ne 0 ]
then
echo "You did not supplied number value"
exit 1
fi
[ $num -lt 2 ] && echo "Values < 2 are not prime numbers" && exit 1

i=2
while [ $i -lt $num ]
do
if [ `expr $num % $i` -eq 0 ]
then
echo "$num is not a prime number"
echo "Since it is divisible by $i"
exit
fi
i=`expr $i + 1`
done
echo "$num is a prime number "

$ ./primecheck
Enter a number: 17
17 is a prime number

No comments:

Post a Comment