Challenge 1: Multiplication Table of a Number
Let's try printing the multiplication table of a number in this lesson.
We'll cover the following
Problem statement
Write a piece of code that will:
- Print the multiplication table of a number
$num
up to 10. - You have to use
while
loop for solving this problem. - Your code should return the string
$ans
with all the values computed from multiplication appended in that string.- Here’s a link showing how you can add values to a string.
Input
An integer.
Output
A string.
Sample Input:
5
Sample output
Here’s an illustration showing what the string $ans
should have stored in it at the end of the while
loop for the above mentioned input.
Note: Just like it is shown in the picture above, the values in the string should have spaces in between them. You can add a space by simply adding quotation marks with space in between them to your string, like this: " ".
Coding challenge
Write your code below. It is recommended that you try solving the exercise yourself before viewing the solution.
Good Luck!
# $num is available as number passed to print the table# initialize variable to use in while loop$ans = "xyz"; #return the correct value in this string#write your code for while loop herereturn $ans;