944,033 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1955
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 19th, 2007
0

problem with loop

Expand Post »
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???
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hoceandress is offline Offline
12 posts
since Aug 2007
Aug 19th, 2007
0

Re: problem with loop

So, you are having trouble with language constructs or logic?
Last edited by SpS; Aug 19th, 2007 at 10:49 pm.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 19th, 2007
0

Re: problem with loop

c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hoceandress is offline Offline
12 posts
since Aug 2007
Aug 20th, 2007
0

Re: problem with loop

The statement
C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 20th, 2007
0

Re: problem with loop

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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jsap is offline Offline
4 posts
since Aug 2007
Aug 20th, 2007
0

Re: problem with loop

#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
c++ Syntax (Toggle Plain Text)
  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.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005
Aug 20th, 2007
0

Re: problem with loop

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

For example:
C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 85
Solved Threads: 45
Posting Whiz in Training
dougy83 is offline Offline
275 posts
since Jun 2007
Aug 20th, 2007
0

Re: problem with loop

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:
c++ Syntax (Toggle Plain Text)
  1. int e;
  2. for (e=2; e<=n; e+=2)
Last edited by darkagn; Aug 20th, 2007 at 3:02 am.
Reputation Points: 395
Solved Threads: 192
Veteran Poster
darkagn is offline Offline
1,136 posts
since Aug 2007
Aug 20th, 2007
0

Re: problem with loop

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...
Reputation Points: 10
Solved Threads: 1
Newbie Poster
hoceandress is offline Offline
12 posts
since Aug 2007
Aug 20th, 2007
0

Re: problem with loop

Click to Expand / Collapse  Quote originally posted by darkagn ...
Minor point: don't forget that in C++ you must initialise your variable outside the for loop:
c++ Syntax (Toggle Plain Text)
  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.
SpS
Reputation Points: 70
Solved Threads: 32
Posting Pro
SpS is offline Offline
598 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Closed surface
Next Thread in C++ Forum Timeline: How to sort alphabet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC