Friday, September 5, 2014

Prime Number Checker in Bash

I'm still learning how to program in Bash or Bourne Again Script that is very useful in system administration in Unix or Linux operating system. There is already some improvements in my understanding how to create different types of script using Bash. Indeed learning Bash scripting programming is a wonderful journey that everyone that is interested will truly enjoy.

In this article I would like to share with you a simple script that will check if the given number by the user is a prime number or not. First thing we must able to understand what is really a prime number. A prime number an be divided evenly only by 1, or itself. And it must be a whole number greater than 1 there are many examples of  prime number one example is 3 that can divided evenly by 1 and itself the same with 5,7,11,13,17,19 and so on and so forth. An example of number that is not a prime number is 8 that is very divisible by 2,4.

I hope you will find my sample bash script useful in your learning bash programming. If you have some questions feel free to send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.

Thank you very much.


Sample Output of Our Program


Nano Text Editor used in writing this program

 
Program Listing

#!/bin/bash

# Prime Number Checker
# Written By Mr. Jake R. Pomperada, MAED-IT
# Operating System : UBUNTU 14.04
# Date : September 5, 2014 Friday


echo -e "\n"
echo -e "\t (====== PRIME NUMBER  CHECKER =====)";
echo -e "\n"

read -p  "Kindly enter a number : " number
echo -e "\n"

a=2
b=0

while [ $a -lt $number ];
 do
   solve=$(($number % $a))

  if [[ $solve -eq $b ]]; then
       echo " The $number is NOT a PRIME NUMBER."
       echo -e "\n"
       exit
  else
    a=$(($a+1))
  fi
done
       echo " The $number is a PRIME NUMBER."
       echo -e "\n"

DOWNLOAD FILE HERE

No comments:

Post a Comment