Get the sum of digits in a number

Today by 9.30pm one of my friend called me and asked a small, a very small question “Machan, how to get the sum of digits in a given number in VB”. This was a simpler question to understand but this has a trick, a simple math trick. After 30 mins of code work I got the simple solution. The main problem I faced was <> operator and commenting in VB because It has been almost 2 years that I finally coded in VB.net

Finally this was my code

    Private Sub button1_clicked(sender As Object, e As EventArgs) Handles Button1.Click
        Dim num, n, r As Integer
        num = 0
        n = Int(TextBox1.Text)
        While n <> 0
            r = n Mod 10
            num += r
            n = 10
        End While
        MessageBox.Show(num)
    End Sub
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s