How to get the even number' sum from an integer input.
var intInput = 10;
Now i want the even' sum. In this case = 2+4+6+8+10 = 30
var evenCount = 0;
if (i % 2==0)
{
evenCount = evenCount + i;
}
How to achieve this?
How to get the even number' sum from an integer input.
var intInput = 10;
Now i want the even' sum. In this case = 2+4+6+8+10 = 30
var evenCount = 0;
if (i % 2==0)
{
evenCount = evenCount + i;
}
How to achieve this?
var evenCount = (intInput / 2) * (intInput / 2 + 1);
This is just twice the sum of all the integers from zero to half the specified number.
2+4+6+8+10 = 2 (1+2+3+4+5)
How about this?
var sum = Enumerable.Range(1,10).Where(x=> x%2==0).Sum();
int intInput=10;
var evenCount = 0;
for (int i=1;i<=intInput;i++)
{
if (i % 2==0)
{
evenCount = evenCount + i;
}
}
Try
var intInput =10;
var evenValueSum = 0;
for(int i=intInput ;i>0;i--)
{
if(i %2 ==0)
{
evenValueSum += i;
}
}
int end = inputNum / 2;
int sum = 0;
for(int i = 1; i <= end; i++)
sum += i * 2;
int evenCount = 0;
int countFrom = 1;
int countTo = 10;
for (int i = countFrom; i <= countTo; i++) {
if (i % 2 == 0) {
evenCount += i
}
}