Home > Backend Development > C++ > body text

Bash program to check if a number is prime

WBOY
Release: 2023-09-23 21:45:04
forward
1326 people have browsed it

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.
Copy after login

Example

Input : 23
Output : The number is prime
Copy after login

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
Copy after login

Output

The number is Prime
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!