To print a hollow inverted pyramid pattern in R, we use nested loops to manage spaces and stars.
In this example,
printHollowInvertedPyramid <- function(numRows) {
for (row in numRows:1) {
pattern <- ''
for (space in 1:(numRows - row)) {
pattern <- paste(pattern, ' ', sep = '')
}
if (row == numRows || row == 1) {
pattern <- paste(pattern, '*', sep = '')
pattern <- paste(pattern, rep('*', 2 * row - 2), sep = '')
} else {
pattern <- paste(pattern, '*', sep = '')
pattern <- paste(pattern, rep(' ', 2 * row - 3), sep = '')
pattern <- paste(pattern, '*', sep = '')
}
print(pattern)
}
}
printHollowInvertedPyramid(5)
********* * * * * * * *
In this tutorial, we learned How to print Hollow Inverted Pyramid Pattern in R language with well detailed examples.