Search⌘ K
AI Features

Solution Review: Multiplication Table of a Number

Explore how to use Perl loops to generate a multiplication table of a given number. Learn to initialize variables, use a while loop for repeated multiplication, and concatenate results into a string for output. This lesson helps you apply loop concepts effectively in Perl programming.

We'll cover the following...

Let’s look at the solution first before jumping into the explanation:

Perl
$num = 11; # change values to print table of your choice
$ans = ""; #returns the computed values in this string
$i = 1;
while($i <= 10){
$answer = $num * $i;
$ans .= $answer . " "; # .= operator append the result in $ans
$i++;
}
print $ans;

Explanation

We first initialized the $num to our input value, whose table will be computed and printed. while runs a total of ten times. In each iteration, the result of multiplication is concatenated with the previous value of the string $ans (which has been initialized as an empty string) and a space. It prints the final answer $ans after the loop completes its execution.