Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have three textboxes, and one button .
The first txtbox is where the first number is entered by the user
The second txtbox is where the second number is entered by the user
The third txtbox is where the sum will appear at.
And a enter button is also on the form.

Can I get some help with the coding please!
A simple 3 or 4 line code will be great, thanks!
Posted
Comments
Maciej Los 3-Dec-14 13:33pm    
Do not repost!!!
Richard Deeming 3-Dec-14 14:21pm    
This was the original version, posted 16 hours ago. The other copy was posted 6 hours ago, so that's the repost. :)
Maciej Los 3-Dec-14 14:33pm    
;)

There's no need for a loop to do this:
C#
public static int SumOfEvenNumbersBetween(int start, int end)
{
    if (end < start) 
    {
        // Swap the arguments if necessary:
        return SumOfEvenNumbersBetween(end, start);
    }
    
    if (start < 0)
    {
        // Handle negative ranges:
        if (end < 0) return -SumOfEvenNumbersBetween(-end, -start);
        return SumOfEvenNumbersBetween(0, end) - SumOfEvenNumbersBetween(0, -start);
    }
    
    // The number of evens between 1 and the start of the range (exclusive).
    int m = (start - 1) / 2;
    
    // The number of evens between 1 and the end of the range (inclusive).
    int n = end / 2;
    
    // The sum of the first n even numbers,
    // minus the sum of the first m even numbers:
    return (n * (n + 1)) - (m * (m + 1));
}

VB.NET
Public Shared Function SumOfEvenNumbersBetween(ByVal startNumber As Integer, ByVal endNumber As Integer) As Integer
    If endNumber < startNumber Then
        ' Swap the arguments if necessary:
        Return SumOfEvenNumbersBetween(endNumber, startNumber)
    End If
    
    If startNumber < 0 Then
        ' Handle negative ranges:
        If endNumber < 0 Then
            Return -SumOfEvenNumbersBetween(-endNumber, -startNumber)
        End If
        
        Return SumOfEvenNumbersBetween(0, endNumber) - SumOfEvenNumbersBetween(0, -startNumber)
    End If
    
    ' The number of evens between 1 and the start of the range (exclusive).
    Dim m As Integer = (startNumber - 1) \ 2
    
    ' The number of evens between 1 and the end of the range (inclusive).
    Dim n As Integer = endNumber \ 2
    
    ' The sum of the first n even numbers,
    ' minus the sum of the first m even numbers:
    Return (n * (n + 1)) - (m * (m + 1))
End Function


The sum of the first n positive even numbers is n × (n + 1).

For positive ranges, you just need to calculate the sum of the first n even numbers, and then subtract the sum of the first m even numbers, where m is the number of even numbers before the start of your range.

For purely negative ranges, you just need to swap and negate the arguments, and then negate the answer. For example, the sum of the even numbers between -9 and -1 is the negative of the sum of the even numbers between 1 and 9.

For ranges which straddle zero, break them into two parts, apply the previous rules, and then combine the results.

The code looks slightly more complicated than the loop / LINQ answers in your reposted version of this question[^], but for large ranges, it will be much faster.

EDIT: Added VB.NET version of the code to match the tags.
 
Share this answer
 
v5
Comments
Maciej Los 3-Dec-14 14:32pm    
Great!
+5!
Richard Deeming 3-Dec-14 14:34pm    
Thanks. I knew you'd see the light! :)
CPallini 3-Dec-14 15:32pm    
I know that (at least the general idea, I didn't elaborate), however it looked to me a bit too 'advanced' for the OP (this is just a persona opinion, of coourse).
I any case, great, well done, have my 5.
Richard Deeming 3-Dec-14 15:34pm    
Thanks. :)
As i mentioned in my comment to the question: do not repost!!!

Here you've got your answer: How do I sum even numbers between two numbers[^]
 
Share this answer
 
Hi Try this
C#
private void button2_Click_1(object sender, EventArgs e)
     {
         textBox3.Text = Convert.ToString(Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text));
     }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900