Scala

Program to print triangle pattern using Scala

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:
#
##
###
####
#####

Share This Post

An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at www.24tutorials.com/sai

Lost Password

Register

Do NOT follow this link or you will be banned from the site!