Solution Review: Multiplication Table of a Number
In this lesson, we'll look at the explanation of the last challenge.
We'll cover the following
Let’s look at the solution first before jumping into the explanation:
$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.
Create a free account to access the full course.
Learn to code, grow your skills, and succeed in your tech interview
By signing up, you agree to Educative's Terms of Service and Privacy Policy