- To print sideway triangle pattern using numbers
User Input: 3
Output:
1 1 2 1 2 3 1 2 1
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 num:Int
trows=2*columns-1
k=1
for(i in 1..trows) {
num=1
for(j in 1..k) {
print(num++ +" ")
}
if(i<columns)k++;
else k--;
println("")
}
}
Enter number of columns: 4 1 1 2 1 2 3 1 2 3 4 1 2 3 1 2 1Program
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) {
num=1;
for(j in 1..k) {
print(num++ +" ");
}
//check for position change at which triangle number value to decrease
if(i<columns)k++;
else k--;
println("");
}
Concept