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 will i do and what is the expression of that???

So, you are having trouble with language constructs or logic?

``````#include <iostream.h>
#include <conio.h>

main()
{
clrscr();
int e,n,x;
e=1;
cout<<"n: ";
cin>>n;

do
{
e=e%2;
cout<<"\nn: "<<e;

}  while ((e<=n) && (e++));``````

this my problem i can't fix it!! where n is a positive number inputted by the user. display the sum of all even numbers form 2 to n??using loop.

The statement

``e=e%2;``

will result in e being equal to the remainder of the division of e by 2. In other words, if e is even, e will become 0, if e is odd, e will become 1, and e will not change after that no matter how many times you loop.
To display the sum of all even numbers, you will need TWO variables, one for the sum and one for the even numbers. You should loop according to the even numbers until you get to n, add add them up using the sum.
Have a go and let us know if you need more help.

Cheers
darkagn

you might not want to use a do while loop in case the user enters a value less than 1.

also e++ doesn't need to be a part of the conditional check

and you need to check if the value of e%2 is a multiple of 2 before printing to the screen

#include <iostream.h>
#include <conio.h>

main()
{
clrscr();
int e,n,x;
e=1;
cout<<"n: ";
cin>>n;

do
{
e=e%2;
cout<<"\nn: "<<e;

} while ((e<=n) && (e++));

this my problem i can't fix it!! where n is a positive number inputted by the user. display the sum of all even numbers form 2 to n??using loop.

There are plenty of problems in your code
1) You are using non-standard headers(iostraem.h,conio.h). You should be sticking to standard headers. You only require <iostream> in your code.
2)Implicit int in main is wrong. main always returns int.
3)clrscr() is again non-standard function. Better not use it.
4) Use more descriptive variable names.
You should be doing something like this

``````#include <iostream>
using namespace std;

int main()
{

int sum = 0,num;
cout<<"Enter the number: ";
cin>>num;

do
{
if(!(num&1))
sum += num;
}
while (--num);
cout<<"sum of even numbers:"<<sum;
}``````

Sequential even numbers are 2 apart, so what's wrong with going from 2 to n in steps of 2?

For example:

``````#include <iostream>
using namespace std;
int main(){
int sum = 0, n = 0;

cout << "Enter n: ";
cin >> n;

for(int e = 2; e <= n; e+=2)
sum += e;

cout << "Sum of even numbers from 2 to " << n << " is " << sum << endl;

return 0;
}``````

Yes dougy83 that is what i was getting at, but I didn't want to give away the answer. :P

Minor point: don't forget that in C++ you must initialise your variable outside the for loop:

``````int e;
for (e=2; e<=n; e+=2)``````

thankxz man!!!thanks for suggestion and the code, it really helps me a lot..

i didn't think the other expression..that's a mess of me!! thanks also to jsap and darkagn...

Minor point: don't forget that in C++ you must initialise your variable outside the for loop:

``````int e;
for (e=2; e<=n; e+=2)``````

That's incorrect. It was only required in C89/90. C++ never had such requirement. Even C99 removes this restriction.

That's incorrect. It was only required in C89/90. C++ never had such requirement. Even C99 removes this restriction.

I stand corrected - apologies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.