Problem statement:- Write a bash script to print pascal triangle.
Input:- Number of row and
Output:-
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
Input:- Number of row and
Output:-
#!/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
Tags:
Shell-scripting
Mua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu tiền
vé máy bay vinh đi hồ chí minh
đặt vé máy bay hồ chí minh hà nội
đi máy bay ra đà lạt
vé máy bay giá rẻ tphcm đi quy nhơn
thuê xe ra sân bay nội bài
combo resort đà lạt
The code doesn't print the output as shown.
ReplyDelete