problem with loop

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

problem with loop

 
0
  #1
Aug 19th, 2007
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???
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: problem with loop

 
0
  #2
Aug 19th, 2007
So, you are having trouble with language constructs or logic?
Last edited by SpS; Aug 19th, 2007 at 10:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

Re: problem with loop

 
0
  #3
Aug 19th, 2007
  1. #include <iostream.h>
  2. #include <conio.h>
  3.  
  4. main()
  5. {
  6. clrscr();
  7. int e,n,x;
  8. e=1;
  9. cout<<"n: ";
  10. cin>>n;
  11.  
  12. do
  13. {
  14. e=e%2;
  15. cout<<"\nn: "<<e;
  16.  
  17. } 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.
Last edited by stymiee; Aug 20th, 2007 at 11:33 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 801
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: problem with loop

 
0
  #4
Aug 20th, 2007
The statement
  1. 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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 4
Reputation: jsap is an unknown quantity at this point 
Solved Threads: 1
jsap jsap is offline Offline
Newbie Poster

Re: problem with loop

 
0
  #5
Aug 20th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: problem with loop

 
0
  #6
Aug 20th, 2007
Originally Posted by hoceandress View Post
#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
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6.  
  7. int sum = 0,num;
  8. cout<<"Enter the number: ";
  9. cin>>num;
  10.  
  11. do
  12. {
  13. if(!(num&1))
  14. sum += num;
  15. }
  16. while (--num);
  17. cout<<"sum of even numbers:"<<sum;
  18. }
Last edited by SpS; Aug 20th, 2007 at 2:17 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: problem with loop

 
0
  #7
Aug 20th, 2007
Sequential even numbers are 2 apart, so what's wrong with going from 2 to n in steps of 2?

For example:
  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4. int sum = 0, n = 0;
  5.  
  6. cout << "Enter n: ";
  7. cin >> n;
  8.  
  9. for(int e = 2; e <= n; e+=2)
  10. sum += e;
  11.  
  12. cout << "Sum of even numbers from 2 to " << n << " is " << sum << endl;
  13.  
  14. return 0;
  15. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 801
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Practically a Posting Shark

Re: problem with loop

 
0
  #8
Aug 20th, 2007
Yes dougy83 that is what i was getting at, but I didn't want to give away the answer.

Minor point: don't forget that in C++ you must initialise your variable outside the for loop:
  1. int e;
  2. for (e=2; e<=n; e+=2)
Last edited by darkagn; Aug 20th, 2007 at 3:02 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 12
Reputation: hoceandress is an unknown quantity at this point 
Solved Threads: 1
hoceandress hoceandress is offline Offline
Newbie Poster

Re: problem with loop

 
0
  #9
Aug 20th, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 598
Reputation: SpS is on a distinguished road 
Solved Threads: 32
SpS's Avatar
SpS SpS is offline Offline
Posting Pro

Re: problem with loop

 
0
  #10
Aug 20th, 2007
Originally Posted by darkagn View Post
Minor point: don't forget that in C++ you must initialise your variable outside the for loop:
  1. int e;
  2. 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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC