All pastes #912441 Raw Edit

Untitled

public text v1 · immutable
·published 2008-02-21 11:43 UTC
rendered paste body
#!/bin/bash
#find whether a number is prime or not
#Author : Mehul Ved
#License : BSD

#Ask user for a number
echo -n "Enter a number greater than 1: "

#read the input and store it in variable num
read num

#store num-1 in count to avoid dividing num by itself
count=$[$num-1]


#if loop to check if input is >=2
if [ $num -lt 2 ]
then
	echo "Invalid Input. Please enter a number greater than or equal to 2."
	exit 1
fi

#run the loop till count is greater than 1
while [ "$count" -gt 1 ]
do
	#divide num by count to find the remainder
	div=$[$num % $count]

	#if remainder is zero than number is not prime
	if [ $div -eq 0 ]
	then
		echo "$num is not prime."
		exit 2
	fi

	#decrement count
	let "count -= 1"
done

#if the program comes out of the loop then num is prime
echo "$num is prime."

#successfully exited
exit 0