How to Write code in visual basic to find the sum of a digit or visual basic program to find the sum of a digit. Another VB(Visual basic 6.0) program to find the sum of a digit before dive into the code let’s understand the problem.
What is the sum of a digit?
The sum of a digit is the addition of all the numbers in a number. For example, I want to find the sum of digits of the number 123 so the answer will be 1+2+3 = 6. Let’s see the visual basic program to find the sum of a digit. If you are beginners will be recommended to check more visual basic programs on numbers or mathematical problem.
Algorithm to find the Sum of digit in Visual basic
- Enter the number (n=123)
- Execute While loop till (n>0)
- Separate each digit and perform addition operation with every iteration
- Print the sum on console
Visual basic (vb) program to find the sum of a digit
Module Module1
Sub Main()
Dim a, n, sum As Integer
a = 123
sum = 0
While a > 0
n = a Mod 10
sum = sum + n
a = a / 10
End While
Console.WriteLine("Sum of digit is: " & sum)
Console.ReadLine()
End Sub
End Module
Output: Sum of digit is: 6