How to calculate sum of each pair of digits from two numbers in VB.Net

How to calculate sum of each pair of digits from two numbers in VB.Net

I have Textbox1.Lines - I want to calculate sum each digit with each digit. I think need substring method, 4+7 / 4+4 / 1+7 / and so on.

Example

Input:

47
14

Expected Output:

Textbox2.Text = 

11
15
8
8
5
11

Not sure if it's the cleanest way, but this code should do the trick:

    'Get all chars
    Dim lstAllChars As List(Of Char) = TextBox1.Text.ToList
    'Filter numeric chars
    lstAllChars = lstAllChars.Where(Function(x) x >= "0"c And x <= "9"c).ToList

    Dim lstSum As New List(Of Integer)
    'Loop each char
    For i As Integer = 0 To lstAllChars.Count - 1
        Dim iFirstInt = CType(lstAllChars(i).ToString, Integer)
        'Add the second char
        For y As Integer = i + 1 To lstAllChars.Count - 1
            Dim iSecondInt = CType(lstAllChars(y).ToString, Integer)
            lstSum.Add(iFirstInt + iSecondInt)
        Next
    Next

    'Edit the textbox
    TextBox1.Clear()

    Dim sb As New Text.StringBuilder
    For Each sum In lstSum
        sb.AppendLine(sum.ToString)
    Next

    TextBox1.Text = sb.ToString

Finding sum of digits of a number until sum becomes single digit , Given a number n, we need to find the sum of its digits such that: Input : 1234 Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum(x) == 10 Hence ans  Sub Main () Console.Title = ("Sum of digits calculator") Dim x, sum As Double x = InputBox ("Enter any number : ") sum = 0 While x > 0 sum += x Mod 10 x = (x / 10) - ((x Mod 10) / 10) End While Console.WriteLine ("The sum of digits = " & sum) Console.ReadKey () End Sub


There are 3 steps in this answer:

  1. Split the input in "lines"
  2. Calculate the sum of the numbers on one line
  3. Show the results

So here's how we'll achieve this:

    Dim originalString As String = "47" & vbNewLine & "14" & vbNewLine & "75"   'example string, you will use your textbox instead
    Dim results As String = ""

    Dim lines As String() = originalString.Split(CChar(Environment.NewLine))

    For Each line As String In lines
        Dim lineSum As Integer = 0

        For Each numberChar As Char In line
            If Asc(numberChar) >= 48 AndAlso Asc(numberChar) < 58 Then  'making sure this is a number and nothing else
                lineSum += Asc(numberChar) - 48 'using the ascii chart to determine the value tu add
            End If
        Next

        If results <> "" Then results &= vbNewLine
        results &= lineSum.ToString
    Next

    MessageBox.Show(results)
    'here you could put the 'results' string in the textbox you use for showing outputs

Next time let us know what you tried and how it failed, this community likes to help people who shows this kind of information. Have fun!

[PDF] 402 VB.NET Programming Language (Practical Question), e.g.: 11 and 13, 17 and 19 are twin prime numbers. 6. Enter two positive integers and calculate and print out the sum of the products of each pair of digits. Here Mudassar Ahmed Khan has explained with examples, how to calculate Sum (Total) of DataTable Columns using C# and VB.Net. This article explains how to calculate Sum (Total) of DataTable Columns using the following two methods. 1. DataTable Compute function. 2. Using LINQ.


Since they're strings to begin with you can use. Left(str,n) n is the number of left characters you want. Right(str,n) n is the number of Right characters you want. Mid(str,n,nn) Mid is like substring where n is the starting char, and nn is the number from start you want from the str.

Dim line1 = "47"

Dim d1 As String = Left(line1,1)   'this is 4

Dim d2 As String = Right(line1,1)  'this is 7

now the math

Dim a1 As Integer = Int(d1) + Int(d2)

or

Dim a1 As Integer = CInt(d1) + CInt(d2)

I think they'll both work.

VBSlip 17_1_2015-16, Write a VB program to enter two positive numbers, calculate the sum of the products of each pair of digits occupying the same position in the two numbers. Since you'll be adding two integers to each other to find the sum, you'll need to prompt Visual Basic to recognize numbers as variables. To do so: Type in Private Sub Button1_Click(sender As Object, e As EventArgs) and press ↵ Enter. Type in Handle(Button1_Click) and press ↵ Enter. Type in Dim sum As Integer and press ↵ Enter.


Professional Visual Basic 2012 and .NET 4.5 Programming, MaxValue – 1 End Sub The results shown in Figure 3-6 look very similar to those from A Decimal value consumes 16 bytes in total and can store a maximum value of After all, it's not often that you need to store a number with 15 digits on both using in the last couple of sections that converts to and from Long values by  The task is to find sum and product of digits of the number which evenly divides the number n. Examples: Input: N = 12 Output: Sum = 3, product = 2 1 and 2 divide 12. So, their sum is 3 and product is 2. Input: N = 1012 Output: Sum = 4, product = 2 1, 1 and 2 divide 1012.


[Solved] C# loop, WriteLine("The sum of the integers between these two numbers is {0} " pairs: (​start + end) * N / 2; for odd number of members, a value of the  Sum of digits program in C#. We can write the sum of digits program in C# language by the help of loop and mathematical operation only. Sum of digits algorithm. To get sum of each digit by C# program, use the following algorithm: Step 1: Get number by user; Step 2: Get the modulus/remainder of the number; Step 3: sum the remainder of the number


integer sum digits in vb.net, 2. If you use extra parentheses for clarity, be sure that the opening and To verify that parentheses are properly paired, start with the innermost pair and draw a line connecting them In the following example the digits indicate the total number so far, working Carefully go over the section of code that does the calculation. About Sum (Summation) Calculator . The Sum (Summation) Calculator is used to calculate the total summation of any set of numbers. In mathematics, summation is the addition of a sequence of any kind of numbers, called addends or summands; the result is their sum or total.