mercredi 3 mai 2017

Creating a number Pyramid in Perl

I need to create a pyramid that is extended one row and number at a time until it reaches the input value. The output must look like the following:

Number to extend to : 6
1
12
123
1234
12345
123456

This is what I have so far:

use strict;
use warnings;

print "Number to extend to : ";
chomp(my $size = <>);
while ($size) {
    for (1 .. $size) {
        print $size;
    }

    print "\n";
    $size--;
}

My output is very off and my pyramid is upside down. How do I flip my pyramid and correct the numbers. I was assuming I could concatenate the output by subtracting then I would just reverse the pyramid, but that didn't work out like I expected.

KMBP:Assignment 13 mypc$ perl pattern2.pl 
Number to extend to : 6
666666
55555
4444
333
22
1

Aucun commentaire:

Enregistrer un commentaire