| | |
problem with loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Aug 2007
Posts: 12
Reputation:
Solved Threads: 1
c++ Syntax (Toggle Plain Text)
#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++));
Last edited by stymiee; Aug 20th, 2007 at 11:33 am. Reason: added code tags
The statement 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
C++ Syntax (Toggle Plain Text)
e=e%2;
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
•
•
•
•
#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.
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)
#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; }
Last edited by SpS; Aug 20th, 2007 at 2:17 am.
•
•
Join Date: Jun 2007
Posts: 275
Reputation:
Solved Threads: 45
Sequential even numbers are 2 apart, so what's wrong with going from 2 to n in steps of 2?
For example:
For example:
C++ Syntax (Toggle Plain Text)
#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. 
Minor point: don't forget that in C++ you must initialise your variable outside the for loop:

Minor point: don't forget that in C++ you must initialise your variable outside the for loop:
c++ Syntax (Toggle Plain Text)
int e; for (e=2; e<=n; e+=2)
Last edited by darkagn; Aug 20th, 2007 at 3:02 am.
•
•
•
•
Minor point: don't forget that in C++ you must initialise your variable outside the for loop:
c++ Syntax (Toggle Plain Text)
int e; for (e=2; e<=n; e+=2)
![]() |
Similar Threads
- Problem with do-while loop (C++)
- a for loop problem (Java)
- problem with MATLAB loop (Legacy and Other Languages)
- Problem with for loop (i think) (Pascal and Delphi)
- Subshell Problem, syntax error...Help please! (Shell Scripting)
- Problem with while loop... probably common (C++)
- how to use loop in this question (C++)
- problem with loop back (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Closed surface
- Next Thread: How to sort alphabet
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





