In this article, we’ll learn to create program of pyramid, half pyramid, inverted pyramid, Pascal’s triangle, Floyd’s triangle , Square and many more in Kotlin.

Example 1: Program to print half pyramid using *

*
**
***
****
*****

Code

fun main() {

    for(i in 1..5)
    
    {
        for(j in 1..i)
        {
            print("*")
        }
        println()
    }
    
}

Example 2: Program to print half pyramid using number

1
22
333
4444
55555

Code

fun main() {

    for(i in 1..5)
    
    {
        for(j in 1..i)
        {
            print(i)
        }
        println()
    }
    
}

Example 3: Program to print half pyramid using number in increasing order

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 

Code

fun main() {
    var counter=1

    for(i in 1..5)
    {
        for(j in 1..i)
        {
            print("$counter ")
            counter=counter+1
        }
        println()
    }
    
}

Example 4: Program to print half pyramid using number with occurrence of number

1
2 3
3 4 5
4 5 6 7
5 6 7 8 9

Code

fun main() {

    for(i in 1..5)
    {
            var counter=i

        for(j in 1..i)
        {
            print(" $counter")
            counter=counter+1
        }
        println()
    }
    
}

Example 5: Program to print half pyramid using number with reverse of number

1
2 1
3 2 1
4 3 2 1
5 4 3 2 1

Code

fun main() {

    for(i in 1..5)
    {
            var counter=i

        for(j in 1..i)
        {
            print(" $counter")
            counter=counter-1
        }
        println()
    }
    
}

Example 6: Program to print half pyramid using alphabet

A 
B C 
D E F 
G H I J 
K L M N O

Code

fun main() {

    val last = 'E'
    var alphabet = 'A'

    for (i in 1..5) {
        for (j in 1..i) {
            print("${alphabet++} ")
        }

         println()
    }    
}

Example 7: Program to print half pyramid using duplicate of alphabet

A 
B B 
C C C 
D D D D 
E E E E E 

Code

 fun main() {

    val last = 'E'
    var alphabet = 'A'

    for (i in 1..5) {
        for (j in 1..i) {
            print("${alphabet+i-1} ")
        }

        println()
    }    
}

Example 8: Program to print half pyramid using occurrence of alphabet

A 
B C 
C D E 
D E F G 
E F G H I

Code

fun main() {

    val last = 'E'
    var alphabet = 'A'

    for (i in 1..5) {
        for (j in 1..i) {
            print("${alphabet+i+j-2} ")
        }

        println()
    }    
}

Example 9: Program to print half pyramid using reverse of alphabet

E 
D E 
C D E 
B C D E 
A B C D E 

Code

 fun main() {
    val last = 'E'
    for (i in 1..5) {
      var alphabet = 'A'+5-i
        for (j in 1..i) {
            print("${alphabet} ")
            alphabet++
        }
        println()
    }    
}

Example 10: Inverted half pyramid using *

*****
****
***
**
*

Code

 fun main() {
    for (i in 1..5) {

      for (j in 1..5-i+1) {
            print("*")
        }
        println()
    }    
}

Example 11: Right Side inverted half pyramid using *

 *****
  ****
   ***
    **
     *

Code

 fun main() {

    for (i in 1..5) {

       for(k in 0..(i-1))
        {
             print(" ")
        }
        
         for (j in i..5) 
         {
            print("*")
         }
       
println()
    }    
}

Example 12: Right Side inverted half pyramid using number

 11111
  2222
   333
    44
     5	

Code

 fun main() {

    for (i in 1..5) {

       for(k in 0..(i-1))
        {
             print(" ")
 
        }
        
         for (j in i..5) {
            print("$i")
        }
       
       println()
    }    
}

Example 13: Right Side inverted half pyramid using increment number at row

 12345
  2345
   345
    45
     5

Code

 fun main() {
    for (i in 1..5) {

      for(k in 0..(i-1))
        {
             print(" ")
 
       }
        
 for (j in i..5) {
            print("$j")
        }
       
  println()
    }    
}

Example 14: Right Side half pyramid using *

    *
   **
  ***
 ****
*****

Code

fun main() {

    for (i in 1..5) {

        for(k in i..(5-1))
         {
                   print(" ")
 
        }
        
        for (j in 1..i) {
            print("*")
        }
      println()
    }    
}

Example 15: Right Side half pyramid using number

    1
   22
  333
 4444
55555

Code

  fun main() {
    for (i in 1..5) {

        for(k in i..(5-1))
         {
                   print(" ")
 
        }
        
        for (j in 1..i) {
            print("$i")
        }

      println()
    }    
}

Example 16: Right Side half pyramid using increment number at row

   1
  23
 456
78910

Code

 fun main() {

    var counter=1

      for (i in 1..4) {

         for(k in i..(4-1))
        {
            print(" ")
        }
        
        for (j in 1..i) {
            print("$counter")
            counter=counter+1}

        println()
    }    
}

Example 17: Square using *

*****
*****
*****
*****
*****

Code

fun main() {
    for(i in 1..5)
    
    {
        for(j in 1..5)
        {
            print("*")
        }
        println()
    }
    
}

Example 18: Square using number

12345
12345
12345
12345
12345

Code

fun main() {
    for(i in 1..5)
    
  {
        for(j in 1..5)
        {
            print(j)
        }
        println()
    }
    
}

Example 19: Square using same number at row

11111
22222
33333
44444
55555

Code

fun main() {

      for(i in 1..5)
        {
        for(j in 1..5)
        {
            print(i)
        }
        println()
    }
    
}

Example 20: Square using number in decreasing order

54321
54321
54321
54321
54321

Code

fun main() {

  for(i in 1..5)
    
    {
        for(j in 1..5)
        {
            print(5-j+1)//formula is n-j+1
        }
        println()
    }
   
}

Example 21: Square using number in increasing order

 1 2 3 4 5
 6 7 8 9 10
 11 12 13 14 15
 16 17 18 19 20
 21 22 23 24 25

Code

fun main() {

    var counter=1
    for(i in 1..5)
    
  {
        for(j in 1..5)
        {
            print(" $counter")
            counter=counter+1
        }
        println()
    }
    
}

Example 22: Square using alphabet

A A A A A 
B B B B B 
C C C C C 
D D D D D 
E E E E E 

Code

fun main() {

    val last = 'E'
    var alphabet = 'A'

    for (i in 1..5) {
        for (j in 1..5) {
            print("${alphabet+i-1} ")
        }

        println()
    }    
}

Example 23: Square using alphabet

A B C D E 
A B C D E 
A B C D E 
A B C D E 
A B C D E 

Code

fun main() {

    val last = 'E'
    var alphabet = 'A'

    for (i in 1..5) {
        for (j in 1..5) {
            print("${alphabet+j-1} ")

        }

        println()
    }    
}

Example 24: Square using alphabet in increment order

A B C D E 
F G H I J 
K L M N O 
P Q R S T 
U V W X Y

Code

fun main() {

    val last = 'E'
    var alphabet = 'A'

    for (i in 1..5) {
        for (j in 1..5) {
            print("${alphabet++} ")
        }

        println()
    }    
}

Example 25: Square using alphabet

A B C D E F 
B C D E F G 
C D E F G H 
D E F G H I 
E F G H I J 

Code

 fun main() {

    val last = 'E'
    var alphabet = 'A'
    for (i in 1..5) {
        for (j in 1..5) {
            print("${alphabet+i+j-2} ")
        }

        println()
    }    
}

Example 26: Program to print full pyramid using *

    *
   ***
  *****
 *******
*********

Code

  fun main() {
    for (i in 1..5) {
      for(k in i..(5-1))
        {
        print(" ")
       }
        
        for (j in 1..2*i-1) {
            print("*")
        }
        println()
    }    
}

Example 27: Program to print full pyramid using number

    1
   222
  33333
 4444444
555555555

Code

fun main() {
    for (i in 1..5) {
      for(k in i..(5-1))
        {
        print(" ")
       }
        
        for (j in 1..2*i-1) {
            print("$i")
        }
        println()
    }    
}

Example 28: Program to print full pyramid using number

    1
   121
  12312
 1234123
123451234

Code

   fun main() {

     for (i in 1..5) {

       for(k in i..(5-1))
        {
        print(" ")
       }
        
        for (j in 1..i) {
            print("$j")
        }

            for (m in 1..i-1) {
            print("$m")
        }

        println()
    }    
}

Example 29: Program to print inverted full pyramid using *

*********
 *******
  *****
   ***
    *

Code

 fun main() {
    for (i in 5 downTo(1)) {
      for(k in i..(5-1))
        {
        print(" ")
       }
       for (j in 1..2*i-1) {
            print("*")
        }
        println()
    }    
}

Example 30: Program to print square with pyramid with number and *

1234554321
1234**4321
123****321
12******21
1********1

Code

 fun main() {

 for (i in 1..5) {
     
        for (j in 1..5-i+1) {
            print("$j")
        }
            for(k in 5 downTo(5-i+2))
        {
        print("*")
       }
        
         for(k in 0..(i-2))
        {
             print("*")
 
        }
        
         for (j in i..5) {
            print("${5-j+1}")
        }
        println()
    } 
    }

You may also like...

0 Comments

No Comment.