How to calculate the next multiple of 10 in VB.NET

1 Answer

0 votes
Imports System

Module Program

    '
    '    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
    '
    Function next_multiple_of_10(n As Integer) As Integer
        Return ((n + 9) \ 10) * 10
    End Function

    '
    '    main:
    '    -----
    '    Demonstrates the function with a sample input.
    '
    Sub Main()
        Dim nums() As Integer = {23, 10, 0, 1841}
        Dim length As Integer = nums.Length

        For i As Integer = 0 To length - 1
            Console.WriteLine(next_multiple_of_10(nums(i)))
        Next
    End Sub

End Module


' run:
'
' 30
' 10
' 0
' 1850
'

 



answered Nov 22, 2024 by avibootz
edited 2 days ago by avibootz
...