Write a Kotlin program that finds the sum of the natural numbers up to a given number. You'll receive an integer 'N' as input, representing the limit of the numbers to be added. Your program should print the calculated sum as the output.
Loops: repeat statement
Calculate the sum of natural numbers up to a given limit
Report a typo
Sample Input 1:
10Sample Output 1:
55Sample Input 2:
100Sample Output 2:
5050Write a program in Kotlin
// Import necessary Kotlin libraries
import java.util.*
fun main(args: Array<String>) {
// Create a scanner object for getting input
val scanner = Scanner(System.`in`)
// Read the input number
val N = scanner.nextInt()
// Variable to store the sum
var sum = 0
// Write a repeat loop here to add the numbers from 1 to N to the sum variable
// Output the sum
println(sum)
}
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.