Bash program to check if the Number is a Prime or not Last Updated : 11 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Given a number, the task is to find whether the given number is prime or not using Bash Scripting. Examples: Input: N = 43 Output: Prime Input: N = 35 Output: Not Prime Prime Numbers: A prime number is a whole number greater than 1, which is only divisible by 1 and itself. First few prime numbers are : 2 3 5 7 11 13 17 19 23 ..... Approach: We run a loop from 2 to number/2 and check if there is any factor of the number. If we find any factor then the number is composite otherwise prime. Implementation: BASH #storing the number to be checked number=43 i=2 #flag variable f=0 #running a loop from 2 to number/2 while test $i -le `expr $number / 2` do #checking if i is factor of number if test `expr $number % $i` -eq 0 then f=1 fi #increment the loop variable i=`expr $i + 1` done if test $f -eq 1 then echo "Not Prime" else echo "Prime" fi Output: Prime Time complexity: O(N) for input N because using a while loop Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article PHP | Check if a number is prime A AnmolAgarwal Follow Improve Article Tags : Misc Linux-Unix Shell Script Practice Tags : Misc Similar Reads Check whether a number is semiprime or not Given a positive integer n. Find whether a number is a semiprime or not. Print True if number is semiprime else False. A semiprime is a natural number that is a product of two prime numbers.Examples : Input: 6Output: TrueExplanation6 is a semiprime number as it is aproduct of two prime numbers 2 and 11 min read Bash shell script to find if a number is perfect or not In this article, we will discuss how to write a bash script to find if a number is perfect or not. A perfect number is defined as, a positive number that is equal to the sum of its proper divisors. Smallest no is 6 ex= 1,2,3 are divisor of 6 and 1+2+3=6 Method 1: Using While LoopRead the input using 3 min read Program to check for Twin Prime Numbers A Twin prime are those numbers which are prime and having a difference of two ( 2 ) between the two prime numbers. In other words, a twin prime is a prime that has a prime gap of two. Sometimes the term twin prime is used for a pair of twin primes; an alternative name for this is prime twin or prime 6 min read PHP | Check if a number is prime A prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself. The smallest prime number is 2, which is the only even prime number. All other even numbers are not prime because they are divisible by 2.2 is prime because it can only be divided by 1 and 2.3 is 3 min read How to Check if a Variable Is a Number in Bash A number contains a combination of digits from 0-9. All the variables in Bash are treated as character strings but the arithmetic operations and comparisons are allowed even when it is stored in the string format. But before performing any arithmetic operations, we need to check if the stored value 5 min read Check if a number is Full Prime A full prime number is one in which the number itself is prime and all its digits are also prime. Given a number n, check if it is Full Prime or not.Examples : Input : 53 Output : Yes Explanation: Number 53 is prime and its digits are also prime. Input : 41 Output : No Explanation: Number 41 is prim 7 min read Like