Kotlin Program to Display Factors of a Number

Example: Factors of a Positive Integer

fun main(args: Array<String>) {
    val number = 60

    print("Factors of $number are: ")
    for (i in 1..number) {
        if (number % i == 0) {
            print("$i ")
        }
    }
}

When you run the program, the output will be:

Factors of 60 are: 1 2 3 4 5 6 10 12 15 20 30 60

In the above program, number whose factors are to be found is stored in the variable number (60).

The for loop is iterated from 1 to number. In each iteration, whether number is exactly divisible by i is checked (condition for i to be the factor of number) and the value of i is incremented by 1.

Here's the equivalent Java code: Java Program to Display Factors of a Number

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges