Exercise
Function SumDigits
Objetive
Write a Visual Basic (VB.Net) function SumDigits that receives a number and returns any results in the sum of its digits. For example, if the number is 123, the sum would be 6.
Console.Write( SumDigits(123) );
6
Code
Imports System
Public Class exercise113
Public Shared Function SumDigits(ByVal n As Integer) As Integer
Dim number As String = Convert.ToString(n)
Dim sum As Integer = 0
For i As Integer = 0 To number.Length - 1
sum += Convert.ToInt32(number.Substring(i, 1))
Next
Return sum
End Function
Public Shared Sub Main()
Console.WriteLine(SumDigits(123))
End Sub
End Class