Sunday, September 4, 2011

BASIC SHELL PROGRAMMING


BASIC SHELL PROGRAMMING

This is a basic shell program which runs on terminal. It shows the basic operations like how to use 'while ,if , switch case' , how to declare variable , how to print on to console , how to accept values from user , how to  use bc , etc .This menu driven program finds the factorial of a number ,fibonacci series and check if the given input is an armstrong number .

a=1
while [ $a -eq 1 ]
do
   echo "Menu"
   echo "1.FACTORIAL"
   echo "2.FIBONACCI"
   echo "3.Armstrong"
   echo "Choice:"
   read ch

   case $ch in
     1)      echo "Enter the num:"
             read num
             fact=1
             i=$num
             while [ $i -gt 0 ]
             do
                  fact=`echo $i \* $fact|bc`
                  i=`echo $i - 1 |bc`
             done
             echo "Factorail :" $fact
          ;; 
      2)      echo "Enter the num"
              read num
              n=0
              m=1
              echo "Fib Series : 0     1 "
              i=2
              while [ $i -lt $num ]
              do
                   c=`echo $m + $n |bc`
                   echo $c
                   n=$m
                   m=$c
                   i=`echo $i + 1 |bc`
              done
            ;;
      3)      echo "Enter the num"
              read num
              temp=$num
              sum=0
              while [ $temp -ne 0 ]
              do
                  k=`echo $temp % 10 |bc`
                  sum=`echo $sum +  $k \* $k \* $k |bc`
                  temp=`echo $temp / 10 |bc`
              done
              if [ $sum -eq $num ]
               then
                  echo "The num is Armstrong Number"
               else
                  echo "The num is not a Armstrong Number"
              fi
            ;;

       *)      echo "WRONG INPUT"

   esac

 echo "do you want to continue (0/1)? :"
 read a

done


No comments:

Post a Comment