3.Write a shell script to print below pattern 1 1 2 1 1 3 3 1

  #!/bin/bash

# Number of rows for the pattern

rows=3

# Function to calculate binomial coefficient

binomial_coefficient() {

    n=$1

    k=$2

    res=1

    for (( i=0; i<k; i++ )); do

        res=$((res * (n - i) / (i + 1)))

    done

    echo $res

}

# Generate Pascal's Triangle

for (( i=0; i<rows; i++ )); do

    for (( j=0; j<=i; j++ )); do

        # Print binomial coefficient

        echo -n "$(binomial_coefficient $i $j) "

    done

    echo "" # Move to the next line

done


Comments

Popular posts from this blog