Home > Back-end >  what is the simplest way to make christmas tree using 'for'?
what is the simplest way to make christmas tree using 'for'?

Time:10-25

I want to print

 
   
     
       
         

in the easiest way using 'for'. similarly,

#
 #
  #
   #
    #
     #

I want to print this in similar structure with code above

what I tried :

for(i in 1:5){
  cat(("*"*i), "/n")
}

but it's wrong because it's non-numeric argument.

CodePudding user response:

You're almost there! just change cat(("*"*i), "/n") to cat(rep(" ", i), "\n"). Use rep and use \ instead of /.

> for(i in 1:5) {
    cat(rep(" ", i), "\n")
    }
  
    
      
        
          

CodePudding user response:

You could use rep

for(i in 1:5){
print(rep('*', i), quote=FALSE)
}
  • Related