Learn R Online
 Home            

Multiplication Table in R

Learn how to print multiplication table in R from 1 to 10 of user entered number. Application of for loop and user input are explained here.

Example of Multiplication Table

# This R Program prints Multiplication Table from 1 to 10

#  For loop is used and take input from the user

number <- as.integer(readline(prompt = "Please Enter a Number for Table"))

# For iterating from one to ten, for loop is used

for( t in 1:10)

{

    print ( paste ( number, '*', t, '=', number * t))

}

and the output is

[1] "3 x 1 = 3"
[1] "3 x 2 = 6"

[1] "3 x 3 = 9"
[1] "3 x 4 = 12"
[1] "3 x 5 = 15"
[1] "3 x 6 = 18"
[1] "3 x 7 = 21"
[1] "3 x 8 = 24"
[1] "3 x 9 = 27"
[1] "3 x 10 = 30"

Explanation

1. number: It is the variable which will store value entered by user for multiplication table.

2. as.integer( ): This function will convert the value entered by user from string to integer.

3. readline( ): It is used to take input from user.

4. prompt: This is the prompt or line of text to guide user what to enter and for what purpose.

5. for: It is keyword here for executing for loop in program.

6. print: This function is used to print on console.

7. paste: paste function is used to concatenate text and variables seperated by commas

Exercise

1. Write a program to write multiplication table of a number from 1 to 20.

2. Write a program to write table from 5 to 15.

3. Write a program to write table of any decimal number.

Using a function to write table in RStudio

You can write a function and then call it again and again to write multiplication tables of different numbers. Can you write a program to write tables of numbers from 1 to 20? Try it.

Here is a table function example in RStudio

Multiplication Table Function

Learn to Shine

  • Programming made Easy for all
  • High School and College
  • University level
  • Professionals doctors, engineers, scientists
  • Data Analysts