Multiplication Table in R Programming Language

Multiplication Table in R Programming Language

Multiplication Table: A multiplication table is a grid that displays the products of two numbers, typically ranging from 1 to 10 or any specified range. Each cell in the table represents the result of multiplying the corresponding row and column numbers.

R Programming for Multiplication Table: In R, you can use nested loops to generate a multiplication table. The outer loop iterates through the rows, and the inner loop iterates through the columns, calculating and printing the product of each pair.

 

Input:

num = as.integer(readline(prompt = "Enter a number: "))
# use for loop to iterate 10 times
for(i in 1:5) {
print(paste(num,'x', i, '=', num*i))
}

Explanation:

  • Set the start and end variables to define the range of the multiplication table.
  • Use nested loops to iterate through rows and columns.
  • Calculate the product of the current row and column and display it using cat.
  • The \t adds a tab between the results to format the table.

Final Note: Understanding how to generate a multiplication table in R is a foundational skill. This program can be customized for different ranges and modified for specific needs in data analysis or educational applications.

Output:

Enter your number: 8 [1] “8 x 1 = 8” [1] “8 x 2 = 16” [1] “8 x 3 = 24” [1] “8 x 4 = 32” [1] “8 x 5 = 40”

Final Note:

Understanding how to generate a multiplication table in R is a foundational skill. This program can be customized for different ranges and modified for specific needs in data analysis or educational applications.

Scroll to Top