Skip to content

Instantly share code, notes, and snippets.

@pchrysa
Last active August 1, 2017 21:31
Show Gist options
  • Save pchrysa/cf0a8d7fc7f9909880eb11a09d2d5484 to your computer and use it in GitHub Desktop.
Save pchrysa/cf0a8d7fc7f9909880eb11a09d2d5484 to your computer and use it in GitHub Desktop.
Floyd's triangle in Kotlin
/*
Floyd's triangle lists the natural numbers in a right triangle aligned to the left where
- the first row is 1
- successive rows start towards the left with the next number followed by successive naturals listing one more number than the line above.
The first few lines of a Floyd triangle (n=5) looks like this:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Task
- Write a program using a function to generate and display the first n lines of a Floyd triangle (where n is the number of rows).
- Ensure that the numbers line up in vertical columns as shown in the example above.
*/
fun main(args: Array<String>) {
floydTriangle(5)
}
fun floydTriangle(n: Int) {
var num = 1
for (i in 0..n-1) {
for (y in n-i..n ) {
print("${num++} \t")
}
println()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment