Bash Script: Generate Pascal triangle

Problem statement:- Write a bash script to print pascal triangle.
Input:- Number of row and
Output:- 
Wiki:- In mathematicsPascal's triangle is a triangular array of the binomial coefficients

#!/bin/bash
#generate pascal triangle
echo -n "Enter the number of Row "
read NR

typeset -A arr
#declare -a arr

for i in `seq 0 $NR`;do
 arr[$i,0]=1 #start is 1
 arr[$i,$i]=1  #end is 1
 p=$((i-1))
 #for j in `seq 1 $p`;do 
  for ((j=1;j<$i;j++));do
   a=${arr[$((i-1)),$((j-1))]}
   b=${arr[$((i-1)),$j]}
   arr[$i,$j]=$((a+b))
 done
#echo ${arr[$i]}
done
#print result
for ((i=0;i<=$NR;i++));do
for((j=0;j<=$i;j++))
do
  echo -n ${arr[$i,$j]} " "
done
 printf "\n"
done

Sample output:-
[zytham@s158519-vm scripts]$ sh  pgssp8.sh
Enter the number of Row 4
1
1  1
1  2  1
1  3  3  1
1  4  6  4  1

2 Comments

Previous Post Next Post