Bash program to check if the Number is a Prime or not


Bash also known as GNU bash is a command language and unix shell script is a command line interpreter for operating system. It was designed by Brian Fox and was a free software which replaced Bourne shell. It first released in 1989 and some became go to for login shell for linux based operating systems like macOS, Linux based softwares, etc.

Prime number is a number that has only two factors i.e. the number itself and 1. For example, 2 , 3 , 5, 7 , 11 , 13 , 17 , 19 , 23 , 29….

Here we are given a number, and we need to find whether the given number is prime or not.

Input : A number
Output : “The number is prime ” OR “The number is not prime” based on the number.

Example 

Input : 23
Output : The number is prime

ALGORITHM

  • Step 1 − Loop from 2 to n/2, i as loop variable

  • Step 2 − if number is divisible, print “The number is not prime” and flag = 1;

  • Step 3 − if flag != 1, then print “The number is prime”.

  • Step 4 − Exit.

PROGRAM

number=53
i=2
flag=0
while test $i -le `expr $number / 2`
do
if test `expr $number % $i` -eq 0
then
flag=1
fi

i=`expr $i + 1`
done if test $flag -eq 1
then
echo "The number is Not Prime"
else
echo "The number is Prime"
Fi

OUTPUT

The number is Prime

Updated on: 13-Nov-2019

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements