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 –

object PrintTriangle {
def main(args: Array[String]) {
for(i < - 1 to 5){
          for(j <- 0 to i){
              print("#")
          }
          println("")
      }
}
}

Approach 2 –

object PrintTriangle{
def main(args: Array[String]) {
for(x <- 1 until 6) { println("#" * x) }
}
}

Output:
#
##
###
####
#####

Share This Post

Avatar
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.saikumar.me

Lost Password

Register