loop problem?

A program will calculate and display the sum of all even even numbers from 2 to n, where n is a positive number inputted by the user. What should I do and what is the expression of that?
[186 byte] By [codeexpert123] at [2007-11-20 10:13:50]
# 1 Re: loop problem?
Sounds a lot like homework, so a hint is looking at modulus (%).
Homogenn at 2007-11-9 11:34:58 >
# 2 Re: loop problem?
You should write code for it. :)

Take a pen and paper, write down the steps that need to be performed, then write the same steps in C# code.
Shuja Ali at 2007-11-9 11:36:09 >
# 3 Re: loop problem?
Interesting homework. You can solve it without using a loop. ;) It is just a mathematical problem. The solution is quite simple so I believe you can solve it alone.
torrud at 2007-11-9 11:37:08 >
# 4 Re: loop problem?
try this thing in your program
It works.

It doesn't work. Secondly, why are you trying to do this guys homework for him? Anyone with a week of programming experience in any language should be able to knock that out within 30 minutes in c#.

Let him at least put up his own attempt.

Also, don't fix your solution, at least make him do that.
Mutant_Fruit at 2007-11-9 11:38:02 >
# 5 Re: loop problem?
It doesn't work. Secondly, why are you trying to do this guys homework for him? Anyone with a week of programming experience in any language should be able to knock that out within 30 minutes in c#.

Let him at least put up his own attempt.

Also, don't fix your solution, at least make him do that.

How can you say it does'nt work. I have compiled it and seen the result. I should not do his homework, right i will delete for now. But if you say it does'nt work prove it to me , show me in private message. It very easy to just say it does'nt work.
klamath23 at 2007-11-9 11:39:02 >
# 6 Re: loop problem?
A program will calculate and display the sum of all even even numbers from 2 to n, where n is a positive number inputted by the user. What should I do and what is the expression of that?

This is one way you can do it .

int sum = 0;
for ( int i = 2; i <= n; i += 2 ) {
sum += i;
}
klamath23 at 2007-11-9 11:40:13 >
# 7 Re: loop problem?
Well, the problem with your original code (from what i remember, i only glanced at it when i noticed the problem) was that you coded the loop like this:

for(int i=0; i <= n; n++)
DoStuff();

So unless i misread it, it couldn't have worked. Maybe you transcribed it wrong, or maybe i misread. Either way, if that's what you did write, it was a mistake.
Mutant_Fruit at 2007-11-9 11:41:06 >
# 8 Re: loop problem?
Well, the problem with your original code (from what i remember, i only glanced at it when i noticed the problem) was that you coded the loop like this:

for(int i=0; i <= n; n++)
DoStuff();

So unless i misread it, it couldn't have worked. Maybe you transcribed it wrong, or maybe i misread. Either way, if that's what you did write, it was a mistake.

i didnot write the full code because it was his homework problem , i just wanted to give him hint. I will send the complete code to by private message,
This problem is not new this is asked many times in c++, java etc.
klamath23
klamath23 at 2007-11-9 11:42:10 >
# 9 Re: loop problem?
Because of post #7 from klamath23 the solution is out now. Therefore I can post my solution too:

public uint Calculate(uint n)
{
//ensure the number is an even number
if (n % 2 == 1)
n -= 1;
//calculate sum without a loop because it is a arithmetic progression
return Convert.ToUInt32(n*(n/4 + 0.5));
}

The complexness is reduced to O(1) from O(n/2) when using a loop.
torrud at 2007-11-9 11:43:08 >
# 10 Re: loop problem?
public uint Calculate(uint n)
{
if (n % 2 == 1)
n -= 1;

return Convert.ToUInt32(n*(n/4 + 0.5));
}

Suppose n=6. The final answer should be 6 + 4 + 2 = 12.

In your code, if n=6, the final answer is 9. That's a bit of a problem ;)
Mutant_Fruit at 2007-11-9 11:44:09 >
# 11 Re: loop problem?
6*(6/4+0.5) = 6 * 2 = 12
Zaccheus at 2007-11-9 11:45:08 >
# 12 Re: loop problem?
Suppose n=6. The final answer should be 6 + 4 + 2 = 12.

In your code, if n=6, the final answer is 9. That's a bit of a problem ;)

Try something like this ((n *n)/4 + (n* 0.5))
if n = 6
then ((6*6)/4 + (6 *0.5)) = 12
klamath23 at 2007-11-9 11:46:17 >
# 13 Re: loop problem?
Yes, but isn't n*(n/4 + 0.5) exactly the same as (n *n)/4 + (n* 0.5) ?
Zaccheus at 2007-11-9 11:47:13 >
# 14 Re: loop problem?
6*(6/4+0.5) = 6 * 2 = 12

This works for human, this does'nt work in c# , so if you try something like

((n *n)/4 + ( n * 0.5)) = 12 in c#.
klamath23 at 2007-11-9 11:48:20 >
# 15 Re: loop problem?
Yes, but isn't n*(n/4 + 0.5) exactly the same as (n *n)/4 + (n* 0.5) ?

if you know algebra then you should know this
c * (a + b) = c * a + c * b;
2 * (3 + 5) = 2 * 3 + 2 *5;
16 = 16 ;
klamath23 at 2007-11-9 11:49:12 >
# 16 Re: loop problem?
Indeed.

This works for human, this does'nt work in c#.

You are correct, because it is integer division!

It would wouk if he wrote n / 4.0.
:D
Zaccheus at 2007-11-9 11:50:17 >
# 17 Re: loop problem?
6*(6/4+0.5) = 6 * 2 = 12
Ah, but you're performing integer division, not floating point division.

6/4 == 1

EDIT: It won't actually be completly accurate if you just change it to 4.0. You may end up with an answer like 11.99999999 instead of 12.

To round a floating point number to the *nearest* integer what you need to do is add 0.5 then just cast the float to int.

i.e.

double result = n * (n / 4.0 + 0.5);
int finalAnswer = (int)(result + 0.5);

Alternatively one of the other forms of the equation would be a better way of doing it.
Mutant_Fruit at 2007-11-9 11:51:14 >
# 18 Re: loop problem?
Well, as it happens 1.5, can be accurately reprepresented in binary - but I do take your point. Rounding errors are always an issue in floating point calculations.
:thumb:
Zaccheus at 2007-11-9 11:52:19 >
# 19 Re: loop problem?
Well, as it happens 1.5, can be accurately reprepresented in binary - but I do take your point. Rounding errors are always an issue in floating point calculations.
:thumb:

There are better ways to do this problem
like this

int sumEven = 0;
for(int i =2; i<=n;n++)
{
if((i % 2)== 0)
sumEven+=i;
}

Console.WriteLine(" the sum of even numbers till n is:"+sumEven);
klamath23 at 2007-11-9 11:53:18 >
# 20 Re: loop problem?
Indeed.

You are correct, because it is integer division!

It would wouk if he wrote n / 4.0.
:D

You are right. It was just a typo mistake of me because I did not test the code. It means n / 4.0 on that part.

But I am understand the point of view for a rounding error. There is a simple solution. You have only to eliminate the fraction inside.

public uint Calculate(uint n)
{
//ensure the number is an even number
if (n % 2 == 1)
n -= 1;
//calculate sum without a loop because it is a arithmetic progression
return n*(n + 2) >> 2;
}

That's it. There should no rounding error any longer. :wave:
torrud at 2007-11-9 11:54:21 >
# 21 Re: loop problem?
thanks for the help but I myself solved it:D
codeexpert123 at 2007-11-9 11:55:18 >