Home  >  Article  >  Backend Development  >  Bash program to check if a number is prime

Bash program to check if a number is prime

WBOY
WBOYforward
2023-09-23 21:45:041332browse

Bash program to check if a number is prime

Bash (also known as GNU bash) is a command language and Unix shell script, a command line interpreter for the operating system. Designed by Brian Fox, it is a free software alternative to the Bourne shell. It was first released in 1989 and became the login shell of choice for macOS, Linux-based operating systems, and other Linux-based software.

Prime number is a number with only two factors, the number itself and 1. For example, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, etc.

Here, we are given a number and need to determine whether the given number is a prime number.

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 - From 2 to n/2 Loop, i as loop variable

  • Step 2 - If the number is divisible, print "The number is not prime" and set the flag to 1;

  • Step 3 - If the flag is not equal to 1, 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

The above is the detailed content of Bash program to check if a number is prime. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete