How to create R Multiplication Table

In this article, we will look into how to write an R program if we are given an input ‘n’ and we need to print its multiplication table.

Creating a multiplication table in R is a fundamental and helpful activity, particularly when you need to see how numbers multiply with one another or for instructional purposes. This tutorial will show you how to create a multiplication table using R, a flexible programming language for mathematical operations and data analysis. You will have a thorough understanding of how to develop a multiplication table in R and how to modify it to suit your needs by the end of this course. Let’s get started and investigate how to make an R multiplication table.

Multiplication Table

Here is an example of the multiplication table.

Input: 5

Output : 5 * 1 = 5

5 * 2 = 10

5 * 3 = 15

5 * 4 = 20

5 * 5 = 25

5 * 6 = 30

5 * 7 = 35

5 * 8 = 40

5 * 9 = 45

5 * 10 = 50

Program to Print Multiplication Table

Now, let us see multiple ways to print the multiplication tables in R. There are two methods: one is using iteration i.e., using a ‘for’ loop and the other one is recursion.

Method 1: Using Iteration

We are going to take an input number from the user. Then, we check if the number is of type numeric. After checking we iterate using the for loop 10 times to multiply with each number, and we display it.

Display multiplication table from 1 to 10

R




# R program to print table for given input
 
# take input from the user
number <- as.numeric(readline("Enter a number: "))
 
# Check if the input is a valid number
if (is.numeric(number)) {
  for (i in 1:10) {
    result <- number * i
    cat(number, "x", i, "=", result, "\n")
  }
} else {
  cat("Please enter a valid numeric value.\n")
}


Output:

Enter a number: 5
5 x 1 = 5 
5 x 2 = 10 
5 x 3 = 15 
5 x 4 = 20 
5 x 5 = 25 
5 x 6 = 30 
5 x 7 = 35 
5 x 8 = 40 
5 x 9 = 45 
5 x 10 = 50 

Time Complexity: O(1)

Auxiliary Space: O(1)

This simple program helps in printing a simple table from 1 to 10. We can modify it to display according to the desired range by the user. We use the function ‘is_numeric()‘ to check the type of the entered input. Then we use ‘cat()‘ to print the output.

Display multiplication table up to the user’s desired output

R




#program to print the multiplication table
 
# take input from user
number <- as.numeric(readline("Enter a number: "))
range <- as.numeric(readline("Enter the end range: "))
 
# Check if the input is a valid number
if (is.numeric(number)) {
  for (i in 1:range) {
    result <- number * i
    cat(number, "x", i, "=", result, "\n")
  }
} else {
  cat("Please enter a valid numeric value.\n")
}


Output:

Enter a number: 8
Enter the end range: 20
8 x 1 = 8 
8 x 2 = 16 
8 x 3 = 24 
8 x 4 = 32 
8 x 5 = 40 
8 x 6 = 48 
8 x 7 = 56 
8 x 8 = 64 
8 x 9 = 72 
8 x 10 = 80 
8 x 11 = 88 
8 x 12 = 96 
8 x 13 = 104 
8 x 14 = 112 
8 x 15 = 120 
8 x 16 = 128 
8 x 17 = 136 
8 x 18 = 144 
8 x 19 = 152 
8 x 20 = 160 

Time Complexity: O(n), n is the range of numbers -> O(1) as n is always constant

Auxiliary Space: O(1)

This program displays a table up to the user’s desired output. We can make other modifications is to give access to the user to decide both the starting and ending range of output.

Display the multiplication table of the user’s desired range

R




#program to print the multiplication table
 
# take input from the user
number <- as.numeric(readline("Enter a number: "))
range_start <- as.numeric(readline("Enter the start range: "))
range_end <- as.numeric(readline("Enter the end range: "))
 
# Check if the input is a valid number
if (is.numeric(number)) {
  for (i in range_start:range_end) {
    result <- number * i
    cat(number, "x", i, "=", result, "\n")
  }
} else {
  cat("Please enter a valid numeric value.\n")
}


Output:

Enter a number: 12
Enter the start range: 5
Enter the end range: 12
12 x 5 = 60 
12 x 6 = 72 
12 x 7 = 84 
12 x 8 = 96 
12 x 9 = 108 
12 x 10 = 120 
12 x 11 = 132 
12 x 12 = 144 

Time Complexity: O(n-m), n is the end of the range of numbers, m is the start of the range -> O(1) because it will be a constant anyways

Auxiliary Space: O(1)

This R script requests a number and a desired range from the user before determining whether the input is numerical. It computes and prints the multiplication table inside the given range using a “for” loop if it is valid. It asks the user for a legitimate numeric value if the input is not numerical.

Method 2: Using Recursion

Program: Display multiplication by user input

R




# Function to display a multiplication table for a given number
display_multiplication_table <- function(number, i = 1) {
  if (i > 10) {
    return()
  }
  result <- number * i
  cat(number, "x", i, "=", result, "\n")
  display_multiplication_table(number, i + 1)
}
 
# Prompt the user for a number
number <- as.numeric(readline("Enter a number: "))
 
# Check if the input is a valid number
if (is.numeric(number)) {
  cat("Multiplication Table for", number, ":\n")
  display_multiplication_table(number)
} else {
  cat("Please enter a valid numeric value.\n")
}


Output:

Enter a number: 18
Multiplication Table for 18 :
18 x 1 = 18 
18 x 2 = 36 
18 x 3 = 54 
18 x 4 = 72 
18 x 5 = 90 
18 x 6 = 108 
18 x 7 = 126 
18 x 8 = 144 
18 x 9 = 162 
18 x 10 = 180 
NULL

Time Complexity: O(1)

Auxiliary Space: O(1)

The multiplication table for a given number up to 10 can be printed using the recursive function display_multiplication_table defined in this R code. It asks for input from the user, determines whether it’s numerical, and then calls the function to show the table. It asks the user for a valid numeric value if the input is not numerical.

Conclusion

A fundamental R assignment that is beneficial for both academic and practical purposes is the creation of multiplication tables. In order to print tables, this article explains two techniques: iteration using a “for” loop and recursion. Tables can be tailored by users according to input and range preferences, making it a flexible tool for a variety of applications.



Contact Us