#2. Write a function Triangle which has one parameter n, it will print a Triangle of stars with nxn. By default, n is set to 3. For example, calling Triangle(3) or Triangle() will print the following:
# *
# * *
# * * *
#my code down below is not working and I have no idea why. Could someone help?
Triangle <- function(n){
for(i in 0:(n-1)){
s <- ""
for(k in 0:(n-1))
s <- paste(s, " ", sep = "*")
for(j in 0:i){
s <- paste(s, sprintf("%3d", choose(i, j)), sep = "*")}
}
}
print(Triangle(3))
code :
raw_code :
#function to print n*n asterisk
Triangle <- function(n){
for(i in 0:(n-1)){
s <- ""
for(k in 0:i+1){
s <- paste(s," ",sep="*")
}
print(noquote(s)) #printing without quotes
}
}
***do comment for queries and rate me up****
Get Answers For Free
Most questions answered within 1 hours.