# C++ adding evens and odds

#### SqrachMasda

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 ?

Related Computing and Technology News on Phys.org

#### dduardo

Staff Emeritus
You can do something like this:

(input%2) ? (even+=input) : (odd+=input) ;

#### gerben

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.

#### himanshu121

SqrachMasda said:
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 ?
i believe you will need iostream.h only if working on Console
can be done in following way

#include<iostream>

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

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

cin>>ch;

return 0;

}

#### dduardo

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

Code:
#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:

#### NateTG

Science Advisor
Homework Helper
dduardo said:
Well, since the solution has already been posted I might as well give you the most optimized C version.
Shouldn't you be using
Code:
(i&1)
rather than
Code:
(i%2)
Just in case the compiler doesn't do that?

#### dduardo

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 ;

#### himanshu121

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
Thanks

#### dduardo

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

http://maven.smith.edu/~thiebaut/ArtOfAssembly/artofasm.html

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:

#### NateTG

Science Advisor
Homework Helper
himanshu121 said:
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
Thanks
You should worry about computational i.e. algoritmic complexity.
The "i&1" vs "i%2" thing is really a joke.

#### egsmith

NateTG said:
You should worry about computational i.e. algoritmic complexity.
The "i&1" vs "i%2" thing is really a joke.
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) {
sum[i&1]+=i;
}

#### gerben

egsmith said:
int sum[2]={0,0}, i=0;
while(i<= read another integer) {
sum[i&1] += i++;
}

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")

#### SqrachMasda

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

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving