In this example, you will learn to print half pyramids, inverted pyramids, full pyramids, inverted full pyramids and Floyd's triangle in Bash scripting.
Half Pyramid of *
*
* *
* * *
* * * *
* * * * *
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for ((i=1;i<=rows;i++))
do
for ((j=1;j<=i;j++))
do
printf "* "
done
printf "\n"
done
|
bash_half_pyramid |
Half Pyramid of 0
Enter num of Rows:5
0
0 0
0 0 0
0 0 0 0
0 0 0 0 0
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for ((i=1;i<=rows;i++))
do
for ((j=1;j<=i;j++))
do
printf "0 "
done
printf "\n"
done
|
bash_half_pyramid-0 |
Half Pyramid of numbers
Enter num of Rows:5
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
num=1
for ((i=1;i<=rows;i++))
do
for ((j=1;j<=i;j++))
do
printf "$num "
num=$((num + 1))
done
num=1
printf "\n"
done
|
Half Pyramid of numbers Bash |
Inverted Half Pyramid of *
Enter num of Rows:5
* * * * *
* * * *
* * *
* *
*
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for ((i=rows;i>=1;i--))
do
for ((j=1;j<=i;j++))
do
printf "* "
done
printf "\n"
done
|
Inverted Half Pyramid of star |
Full Pyramid of *
Enter num of Rows:5
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for((i=1; i<=rows; i++))
do
for((j=1; j<=rows - i; j++))
do
printf " "
done
for((j=1; j<=2*i - 1; j++))
do
printf "* "
done
echo
done
|
Full Pyramid of star |
Inverted Full Pyramid of *
Enter num of Rows:5
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
for((i=rows; i>=1; i--))
do
for((j=1; j<=rows - i; j++))
do
printf " "
done
for((j=1; j<=2*i - 1; j++))
do
printf "* "
done
echo
done
|
Inverted Full Pyramid of star |
Floyd's Triangle
Enter num of Rows:10
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
Script
#!/bin/bash
printf "Enter num of Rows:"
read rows
num=1
for((i=1; i<=rows; i++))
do
for((j=1; j<=i; j++))
do
echo -n "$num "
num=$((num + 1))
done
echo
done
|
Floyds Triangle |
No comments