Advertisement
Shailrshah

Pascal Triangle

Nov 4th, 2014
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.45 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. calcElement(){
  4.     num=1
  5.     for(( a=$i; a>1; a-- )){
  6.         let num*=a
  7.     }
  8.  
  9.     den1=1
  10.     for(( a=$j; a>1; a-- )){
  11.         let den1*=a
  12.     }
  13.            
  14.     den2=1
  15.     for(( a=$i-$j; a>1; a-- )){
  16.         let den2*=a
  17.     }
  18.  
  19.     ans=$(( num / (den1*den2) ))
  20. }
  21.  
  22. echo -n "Enter the number of rows: "
  23. read n
  24.  
  25. for(( i=0; i<$n; i++ )){
  26.     for(( space=$n-$i-1; space>=0; space-- )){
  27.         echo -n " "
  28.     }
  29.     for(( j=0; j<=i; j++ )){
  30.         calcElement                
  31.         echo -n "$ans "
  32.     }
  33.     echo ""
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement