CHECKING FOR PRIME NUMBER
To
write a Shell Program to check if the Number is a Prime Number or a Composite
Number.
ALGORITHM:
Step 1: Start
Step 2: Read the Number as ‘n’
Step 3: Initialize the value of ‘t’ to 0 and solve the
expression i=n-1
Step 4: While ‘i’ is greater than or equal to 2, perform
the following steps.
Step 5: Solve p=n%i
Step 6: Check the condition (p=0) and then solve t=t+1
Step 7: End the If Condition
Step 8: Solve the expression i=i-1
Step 9: If ‘t’ is greater than 0, print “The Number is not
a Prime Number”
Step 10: If the condition is not satisfied, print “The
Number is a Prime Number”
SHELL
PROGRAM:
echo "Enter a Number"
read n
i=`expr $n - 1`
t=0
while [ $i -ge 2 ]
do
p=`expr $n % $i`
if [ $p -eq 0 ]
then
t=`expr $t + 1`
fi
i=`expr $i - 1`
done
if [ $t -gt 0 ]
then
echo "The Number $n is not a Prime
Number"
else
echo "The Number $n is a Prime Number"
fi
SAMPLE
OUTPUT:
[Cseb19@sathish-desktop]# sh
prime.sh
Enter a Number
2
The Number 2 is a Prime Number
[Cseb19@sathish-desktop]# sh
prime.sh
Enter a Number
4
The Number 4 is not a Prime Number
RESULT:
Thus
the Shell Program to check if the given number is prime or not, was executed
and the output was verified.
No comments:
Post a Comment