Click here to Skip to main content
15,886,110 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 even 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!

Example for numbers between 1 and 10 the even number sum is 30
Posted
Comments
Member 11264383 3-Dec-14 11:20am    
because it wasn't answered correctly, I need the sum of the EVEN numbers between two numbers!
Richard Deeming 3-Dec-14 11:22am    
So add comments to the incorrect answers and down-vote them.

Not getting a correct answer to your question isn't an excuse for re-posting the question.
Member 11264383 3-Dec-14 12:16pm    
No
Richard Deeming 3-Dec-14 12:17pm    
If you're not willing to follow the rules, then you're not going to last long around here.

Looks a pretty straightforward task: you just need a loop and the Mod[^] operator.
 
Share this answer
 
Comments
Maciej Los 3-Dec-14 8:21am    
Good general advice!
5ed!
CPallini 3-Dec-14 15:56pm    
Thank you.
See this: https://code.msdn.microsoft.com/windowsdesktop/LINQ-Generation-Operators-8a3fbff7[^]

MSDN wrote:
C#
public void Linq65()
{
    var numbers =
        from n in Enumerable.Range(100, 50)

        select new { Number = n, OddEven = n % 2 == 1 ? "odd" : "even" };

    foreach (var n in numbers)
    {
        Console.WriteLine("The number {0} is {1}.", n.Number, n.OddEven);
    }
}

This sample uses Range to generate a sequence of numbers from 100 to 149 that is used to find which numbers in that range are odd and even.

The number 100 is even.
The number 101 is odd.
The number 102 is even.
The number 103 is odd.
The number 104 is even.
The number 105 is odd.
The number 106 is even.
...
The number 149 is odd.


Based on above example, you can achieve that in two ways (using Linq):
C#
int fromNumber = 10;
int toNumber = 30;
//1. querable method
var SumOfEvenNumbersInRange = (from n in Enumerable.Range(fromNumber, toNumber - fromNumber +1)
        where(n%2==0)
        select n).Sum();
//2. Lambda expressions
int res = Enumerable.Range(fromNumber, toNumber - fromNumber +1).Where(n=>n%2==0).Sum();


In both cases result is the same: 220.
 
Share this answer
 
v4
Comments
CPallini 3-Dec-14 8:09am    
5. Nice.
Maciej Los 3-Dec-14 8:20am    
Thank you, Carlo ;)
Maciej Los 3-Dec-14 13:40pm    
There is few ways to resolve this issue. One of them is to use Linq.
Richard Deeming 3-Dec-14 13:43pm    
Well, you can solve it using LINQ. But for large ranges, the performance won't be great. :)

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