Kotlin Program:

- To print sideway triangle pattern using numbers

Example:

User Input: 3

Output:

A
A B
A B C
A B
A
fun main(args: Array<String>) {
  print("Enter number of columns : ") 
  val userInput = readLine()!!
  var columns:Int =userInput.toInt()
  var trows:Int
  var k:Int
  var c:Char

  trows=2*columns-1
  k=1
  for(i in 1..trows) {
    c='A'
    for(j in 1..k) {
      print(c++ +" ")
    }

    if(i<columns)k++;
    else k--;

    println("")
  }
}

Output:

Enter number of columns: 4

A
A B
A B C
A B C D
A B C
A B
A
Program
pattern

Main Logic :

trows=2*columns-1;   // trows : total rows to print
k=1;  // Used to trigger decreasing order of number

for(i in 1..trows) {
  c='A'
  for(j in 1..k) {
    print(c++ +" ");
  }
  //check for position change at which triangle number value to decrease
  if(i<columns)k++;
  else k--;

  println("");
}

You can print the same pattern in small alphabets, just set c='a'

Concept

Coming Soon !

Quick
Tutorial

Quote

Learn from yesterday, live for today, hope for tomorrow. The important thing is not to stop questioning.