Showing posts with label even odd in bash. Show all posts
Showing posts with label even odd in bash. Show all posts

Wednesday, September 3, 2014

Even and Odd Number Checker in Bash

In this article I would like to share with you a simple program that uses BASH or Bourne Again Shell in Linux operating system I called this program Odd and Even number Checker.  What this program will do is to ask the user to enter a number and then the program will check and determine if the number odd or even number. What is good about this program is that after the program determine the number whether it is even or odd number. The program will ask the user to continue using the program or not if the user choose y for yes the program will run again, if no the program will terminate and will return to command line.

If hope you will find my work useful in learning how to program using Bash scripting language in Linux, Unix or others distributions. If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.

Thank you very much.


Nano text editor is used in writing the bash program


Change File Permission and Running our Bash Script Program



Sample Output of Our Program


Program Listing

#!/bin/bash

# Odd and Even Number Checker
# Written By Mr. Jake R. Pomperada, MAED-IT
# Operating System : UBUNTU 14.04
# Date : September 3, 2014

start()
{
clear
echo -e "\n"
echo -e "\t (====== ODD AND EVEN NUMBER CHECKER =====)";
echo -e "\n"

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

check_number=$(($number % 2))

if [ $check_number == 0 ]; then
   echo "$number is an even number."
else
   echo  "$number is an odd number."
fi
while true; do
  echo -e "\n"
  read -p "More (Y/N)? " answer
  case $answer in
       [Yy]* ) start;  break;;
       [Nn]* ) bye; break;;
       * ) echo "Please answer yes or no. ";;
  esac
done
}

bye()
{
echo -e "\n"
echo -e "\t Thank You For Using This Program.";
echo -e "\n\n";
}

start