WebHosting

Wednesday, June 13, 2012

Print a triangle using shell script


Print numbers in order :

#!/bin/bash
for i in $(seq 0 4)
do
for j in  $(seq $i -1 0)
do
    echo -n $j
done
echo
done

Will give you output as :




0
01
012
0123
01234


Print numbers in reverse order :


#!/bin/bash
for i in $(seq 0 4)
do
for j in  $(seq $i -1 0)
do
    echo -n $j
done
echo
done

Will give output as :


0
10
210
3210
43210

If you want to print star triangle :


#!/bin/bash
for i in $(seq 0 4)
do
for j in  $(seq $i -1 0)
do
    echo -n "*"
done
echo
done

Output :


*
**
***
****
*****






No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

Warp AI Terminal 🔥 | The Future of Coding? Agentic Dev Environment Explained!

  Warp AI Terminal 🔥 | The Future of Coding? Agentic Dev Environment Explained! What if your terminal could think, assist, and even act li...