Bash Script: Generate Pascal triangle

Problem statement:- Write a bash script to print pascal triangle.
Input:- Number of row and
Output:- 
Wiki:- In mathematics, Pascal'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

6 Comments

  1. The code doesn't print the output as shown.

    ReplyDelete
  2. If you're exploring different websites today, here's another one to add to your list. Vaporae is an online destination connected with the vaping category. Visit the website to discover its brand presence and review the information available to visitors. Exploring official platforms can help you understand a brand better. Remember to research responsibly and consider relevant safety guidance. https://vaporae.com/

    ReplyDelete
Previous Post Next Post