Answers

Question and Answer:

  Home  Electrical Engineering

⟩ Write a function to output a diamond shape according to the given (odd) input?

Examples: Input is 5 Input is 7

* *

*** ***

***** *****

*** *******

* *****

***

*

### BEGIN PERL SNIPET ###

for ($i = 1; $i <= (($input * 2) - 1); $i += 2) {

if ($i <= $input) {

$stars = $i;

$spaces = ($input - $stars) / 2;

while ($spaces--) { print " "; }

while ($stars--) { print "*"; }

} else {

$spaces = ($i - $input) / 2;

$stars = $input - ($spaces * 2);

while ($spaces--) { print " "; }

while ($stars--) { print "*"; }

}

print "n";

}

### END PERL SNIPET ###

 190 views

More Questions for you: