Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ adding evens and odds

  1. Jun 15, 2005 #1
    i have to create a program to seperate ANY number of integers into odds and evens and give back the sums of them seperately

    i'm thinking to use if i%2=0 then it is even
    or if i%2 is .5 then it is odd or sumthing
    i haven't learned much about how to set up these loop things so i dont know how to organize it and what headerfiles to use
    any body got any hints, help, or answers :biggrin: ?
  2. jcsd
  3. Jun 15, 2005 #2


    User Avatar
    Staff Emeritus

    You can do something like this:

    (input%2) ? (even+=input) : (odd+=input) ;
  4. Jun 15, 2005 #3
    if i%2 == 0 i is even
    if i%2 == 1 i is odd

    I guess you should know how to make a loop going throug all i. Whithin that loop you check whether i is even or odd, and if the numuber is even you add it to total_even and if it is odd you add it to total_odd.

    For that you do not need any headerfiles. If you want to print the results you need the iostream headerfile.
  5. Jun 15, 2005 #4
    i believe you will need iostream.h only if working on Console
    can be done in following way


    using namespace std;
    int main()
    int i,sumEVEN=0,sumODD=0;
    char ch;
    cout<<"\n"<<"Enter the Number::";
    sumEVEN += i;
    sumODD += i;
    cout<<"Got more integers(Y/N)";
    }while(ch == 'y' || ch == 'Y');

    cout<<"\n"<<"Even Sum::"<<sumEVEN;
    cout<<"\n"<<"Odd Sum ::"<<sumODD;


    return 0;

  6. Jun 15, 2005 #5


    User Avatar
    Staff Emeritus

    Well, since the solution has already been posted I might as well give you the most optimized C version.

    Code (Text):

    #include <stdio.h>

    int main(void) {

    int i,e=0,o=0;

    do {
      printf("Enter a postive integer (-1 to quit): ") ;
      scanf("%i", &i) ;
      if(i>=0) (i&1) ? (o+=i) : (e+=i) ;
    } while(i!=-1) ;

    printf("Even Sum: %i\n", e) ;
    printf("Odd Sum: %i\n", o) ;

    return 0 ;

    Last edited: Jun 15, 2005
  7. Jun 15, 2005 #6


    User Avatar
    Science Advisor
    Homework Helper

    Shouldn't you be using
    Code (Text):

    rather than
    Code (Text):

    Just in case the compiler doesn't do that?
  8. Jun 15, 2005 #7


    User Avatar
    Staff Emeritus

    NateTG, your right, i&1 is more efficient. I changed my source to reflect the change. Good ol' bitwise operators.

    i&1 = 0 when i is even
    i&1 = 1 when i is odd

    Also, if people are wondering what this is:

    (i&1) ? (o+=i) : (e+=i) ;

    it is called a ternary operator and can be generalized like this:

    (condition) ? (expression if condition is true) : (expression if condition is false) ;

    It's just a shorthand way to write:

    if( i&1 ) o+=i ;
    else e+=i ;
  9. Jun 15, 2005 #8
    yes gr8 i have just completed basic java and cany say basic C++,
    but still to learn the optimization and complexity.
    Exactly i knew binary addition wud be much faster, but never applied coz maybe i m not professional.

    Any links/tutorial for Complexity and how to increase Efficiecy of programs
  10. Jun 15, 2005 #9


    User Avatar
    Staff Emeritus

    The best way in my opinion is to learn assembly. You really get a sense of what the processor is really doing.


    For pure algorithms start here:

    Introduction to Algorithms by
    Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein
    Last edited by a moderator: Apr 21, 2017
  11. Jun 15, 2005 #10


    User Avatar
    Science Advisor
    Homework Helper

    You should worry about computational i.e. algoritmic complexity.
    The "i&1" vs "i%2" thing is really a joke.
  12. Jun 16, 2005 #11
    Ya, I though that was funny.
    Here is a way to eliminate that pesky branch.
    in a sort of c-psuedo code hybrid

    int sum[2]={0,0}
    while(i<= read another integer) {
  13. Jun 16, 2005 #12

    I wonder if the original poster is interested in nice optimal solutions, assembly, etc. (noticing that the op stated: "i haven't learned much about how to set up these loop things")
  14. Jun 16, 2005 #13
    Thanks for the help, you all are great...I like that Y/N thing, that's new to me
    and it works great
    himanshu121 , thanks
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook