mm24mm mm24mm - 1 month ago 6
Perl Question

How to make this Perl program print in descending order?

This code works but it prints in ascending order. Do I need to change my whole formula?

print "Enter an integer \n";
my $root = <STDIN>;
my @nums = (100..200);
my $i = 0;

# code in while loop executes as long as condition is true
while ( $i < $#nums )
{
print "$nums[$i]\n",if($nums[$i] % $root == 0); $i++;
}

Answer
print "Enter an integer \n";
my $root = <STDIN>; 
my @nums = (100..200);

@nums = reverse @nums; #Just reverse the arrays

my $i = 0;           

# code in while loop executes as long as condition is true 
while ( $i < $#nums ) 
{
    print "$nums[$i]\n",if($nums[$i] % $root == 0); $i++;
}

May be it will helps you.