Kotlin Program to Calculate the Sum of Natural Numbers: In this program, you’ll learn to calculate the sum of natural numbers using for loop and while loop in Kotlin. You’ll also see how ranges can be helpful for solving the problem.
Kotlin Program to Calculate the Sum of Natural Numbers
Source Code
fun main(args: Array<String>) {
val num = 100
var sum = 0
for (i in 1..num) {
// sum = sum+i;
sum += i
}
println("Sum = $sum")
}
Output
Kotlin Program to Calculate the Sum of Natural Numbers