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:
#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 thenf=1 fi #increment the loop variable i=`expr $i + 1` doneif test $f -eq 1 thenecho "Not Prime"elseecho "Prime"fi |
Output:
Prime
Recommended Posts:
- Bash program to check if the Number is a Palindrome
- Bash program to find A to the power B
- Leap Year Program with Bash Shell Scripting in Windows
- Write a bash script to print a particular line from a file
- Fibonacci Series in Bash
- Useful and time saving bash commands in Linux
- Average of given numbers in Bash
- Sorting an array in Bash using Bubble sort
- Simple Calculator in Bash
- Bash shell script to find sum of digits
- Programs for printing different patterns in Bash
- Bash shell script to swap two numbers
- Bash Script to get Low Battery Alert in Linux
- Bash shell script to find out the largest value from given command line arguments
- How to Fix Minimal BASH Like Line Editing is Supported GRUB Error In Linux?
- How to check if a directory or a file exists in system or not using Shell Scripting?
- C program that does not suspend when Ctrl+Z is pressed
- Tr0ll 3 Walkthrough of Check the Flag or CTF Problem
- How to find time taken by a command/program on Linux Shell?
- Linux Kernel Module Programming: Hello World Program
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh

