Tuesday, 10 May 2016

Print a triangle using * in a Bash script

Hello coders! Hows it going? Today we'll see a basic simple shell script to print a triangle using asterisk * only. Here's the code to do that :

#!/bin/bash
# Simple program to print a triangle using * of height 10

star="*"

for i in {1..10}
do
echo "$star";
star="$star *"

done

You see basically, all we are doing is append a * to itself recursively. Here we are using a basic for-loop to print a triangle of height 10. You can change the height as per your requirement, as needed.

Output :

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

* * * * * * * * * *

Cheers!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker