Nested for loop to print multiplication tables up to a certain number.
Last Updated :
21 Aug, 2023
A nested for loop is used to print multiplication tables up to a certain number by generating rows and columns of multiplication results. Here’s the explanation of how to achieve this using nested loops
In this article, we will discuss how to print multiplication tables up to a certain number with its working example in the R Programming Language using R for loop conditions.
Syntax:
max_number <- 10 # Change this to the desired maximum number
for (i in 1:max_number) {
for (j in 1:10) { # Multiplication up to 10
cat(i, "x", j, "=", i * j, "\n")
}
cat("\n") # Print a blank line after each table
}
Example 1: Nested for loop to print multiplication tables
R
max_number <- 5
for (i in 1:max_number) {
for (j in 1:10) {
cat (i, "x" , j, "=" , i * j, "\n" )
}
cat ( "\n" )
}
|
Output:
Multiplication table for 1 :
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
1 x 6 = 6
1 x 7 = 7
1 x 8 = 8
1 x 9 = 9
1 x 10 = 10
Multiplication table for 2 :
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
2 x 10 = 20
Multiplication table for 3 :
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
3 x 10 = 30
Multiplication table for 4 :
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
4 x 10 = 40
Multiplication table for 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
- Set the value of the variable
max_number
to 5.
- Start an outer
for
loop that iterates through numbers from 1 to the value of max_number
.
- Start an inner
for
loop that iterates through numbers from 1 to 10 for each value of i
.
- Using the
cat()
function, print the multiplication expression in the format “i x j = i * j”, followed by a newline character.
- End of the inner
for
loop.
- Print a blank line to separate the multiplication tables for each
i
value.
- End of the outer
for
loop.
Example 2: Nested for loop to print multiplication tables up to a certain number.
R
max_number <- 13
for (i in 11:max_number) {
for (j in 1:10) {
cat (i, "x" , j, "=" , i * j, "\n" )
}
cat ( "\n" )
}
|
Output:
11 x 1 = 11
11 x 2 = 22
11 x 3 = 33
11 x 4 = 44
11 x 5 = 55
11 x 6 = 66
11 x 7 = 77
11 x 8 = 88
11 x 9 = 99
11 x 10 = 110
12 x 1 = 12
12 x 2 = 24
12 x 3 = 36
12 x 4 = 48
12 x 5 = 60
12 x 6 = 72
12 x 7 = 84
12 x 8 = 96
12 x 9 = 108
12 x 10 = 120
13 x 1 = 13
13 x 2 = 26
13 x 3 = 39
13 x 4 = 52
13 x 5 = 65
13 x 6 = 78
13 x 7 = 91
13 x 8 = 104
13 x 9 = 117
13 x 10 = 130
- Set the value of the variable
max_number
to 13.
- Start an outer
for
loop that iterates through numbers from 1 to the value of max_number
.
- Start an inner
for
loop that iterates through numbers from 1 to 10 for each value of i
.
- Using the
cat()
function, print the multiplication expression in the format “i x j = i * j”, followed by a newline character.
- End of the inner
for
loop.
- Print a blank line to separate the multiplication tables for each
i
value.
- End of the outer
for
loop.
Share your thoughts in the comments
Please Login to comment...