Write a program to Print below triangle pattern using Scala?
#
##
###
####
#####
Using Scala functional style of programming it’s very easy to use print patterns than Java. Below is the code for printing the same using Scala for loops.
Approach 1 –
[code lang=”scala”]object PrintTriangle {
def main(args: Array[String]) {
for(i < – 1 to 5){
for(j <- 0 to i){
print("#")
}
println("")
}
}
}
[/code]
Approach 2 –
[code lang=”scala”]object PrintTriangle{
def main(args: Array[String]) {
for(x <- 1 until 6) { println("#" * x) }
}
}
[/code]
Output:
#
##
###
####
#####