To print a hollow pyramid pattern in R, you can use loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
In this example,
printHollowPyramid <- function(rows) {
for (i in 1:rows) {
pattern <- paste(rep(' ', (rows - i) * 2), collapse = '')
for (j in 1:(2 * i - 1)) {
if (j == 1 | j == 2 * i - 1 | i == rows) {
pattern <- paste(pattern, '* ', sep = '')
} else {
pattern <- paste(pattern, ' ', sep = '')
}
}
print(pattern)
}
}
printHollowPyramid(5)
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in R language with well detailed examples.