Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to calculate the next multiple of 10 in Kotlin

1 Answer

0 votes
/*
    next_multiple_of_10:
    --------------------
    Returns the next multiple of 10 greater than or equal to n.

    Algorithm:
    - Add 9 to n: this ensures that any number not already a multiple of 10
      will "spill over" into the next multiple when integer-divided by 10.
    - Divide by 10: integer division truncates toward zero.
    - Multiply by 10: reconstruct the multiple of 10.

    Example:
        n = 23 → (23 + 9) = 32 → 32 / 10 = 3 → 3 * 10 = 30
        n = 40 → (40 + 9) = 49 → 49 / 10 = 4 → 4 * 10 = 40
*/
fun next_multiple_of_10(n: Int): Int {
    return ((n + 9) / 10) * 10
}

/*
    main:
    -----
    Demonstrates the function with a sample input.
*/
fun main() {
    val nums: IntArray = intArrayOf(23, 10, 0, 1841, 1)
    val length: Int = nums.size

    for (i: Int in 0 until length) {
        println(next_multiple_of_10(nums[i]))
    }
}


/*
run:

30
10
0
1850
10

*/

 



answered Jul 15 by avibootz
...