Loading…
OS · Functions & patterns · Program 28 · In the record
Write a shell script to input a term and print the pascal triangle of that term
1#!/bin/bash2 3echo "Enter the number of terms:"4read n5 6for (( i = 0; i < n; i++ ))7do8 # Leading spaces to center the row9 for (( s = 0; s < n - i - 1; s++ ))10 do11 echo -n " "12 done13 14 # Each value is C(i, j) = C(i, j - 1) * (i - j + 1) / j15 c=116 for (( j = 0; j <= i; j++ ))17 do18 printf "%4d" $c19 c=$((c * (i - j) / (j + 1)))20 done21 22 echo23done5Enter the number of terms:
5
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1Run program 28 in your browser
bash · just-bash. Change the code or the input, run it, break it, and see what happens.