954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with loop

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???

hoceandress
Newbie Poster
12 posts since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

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

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
#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.

hoceandress
Newbie Poster
12 posts since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

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

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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

jsap
Newbie Poster
4 posts since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

#include #include

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

do { e=e%2; cout<<"\nn: "< 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 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;
}
SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 

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;
  }
dougy83
Posting Whiz in Training
275 posts since Jun 2007
Reputation Points: 85
Solved Threads: 45
 

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)
darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

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...

hoceandress
Newbie Poster
12 posts since Aug 2007
Reputation Points: 10
Solved Threads: 1
 

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.

SpS
Posting Pro
599 posts since Aug 2005
Reputation Points: 70
Solved Threads: 32
 
That's incorrect. It was only required in C89/90. C++ never had such requirement. Even C99 removes this restriction.

I stand corrected - apologies.

darkagn
Veteran Poster
1,197 posts since Aug 2007
Reputation Points: 404
Solved Threads: 200
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You