Advertisement
Shailrshah

Fibonacci Series

Nov 2nd, 2014
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.41 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. a=0
  4. b=1
  5. echo -n "Enter the number of terms: "
  6. read n
  7.  
  8. if [ $n -le 0 ]; then
  9.     echo "Error in input"
  10.     exit 0
  11. fi
  12.  
  13. if [ $n -ge 1 ]; then
  14.     echo -n $a " "          #-n used for printing on the same line
  15. fi
  16. if [ $n -ge 2 ]; then
  17.     echo -n $b " "     
  18. fi 
  19. for(( i=2; i<n; i++ )){     #i=2 because 2 numbers have already been printed
  20.     c=$((a+b))      #equivalent to let c=a+b
  21.     echo -n $c " "
  22.     a=$b
  23.     b=$c
  24. }
  25. echo ""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement