How to Create a C++ Program to Separate and Add Even and Odd Integers?

  • C/C++
  • Thread starter SqrachMasda
  • Start date
  • Tags
    C++
In summary, the original poster wants to create a program to separate integers into odds and evens and give back the sums of them seperately. They have not learned much about how to set up these loop things and do not know how to organize it, so they are looking for help. They should worry about computational i.e. algoritmic complexity.
  • #1
SqrachMasda
42
0
i have to create a program to separate 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 don't know how to organize it and what headerfiles to use
any body got any hints, help, or answers :biggrin: ?
 
Technology news on Phys.org
  • #2
You can do something like this:

(input%2) ? (even+=input) : (odd+=input) ;
 
  • #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.
 
  • #4
SqrachMasda said:
i have to create a program to separate 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 don't know how to organize it and what headerfiles to use
any body got any hints, help, or answers :biggrin: ?

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;

}
 
  • #5
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:
  • #6
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?
 
  • #7
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 ;
 
  • #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
Thanks
 
  • #9
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:
  • #10
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.
 
  • #11
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;
}
 
  • #12
egsmith said:
int sum[2]={0,0}, i=0;
while(i<= read another integer) {
sum[i&1] += i++;
}
:smile:

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

1. How can I add all the even numbers in a given range in C++?

To add all the even numbers in a given range in C++, you can use a for loop to iterate through the range and use an if statement to check if the current number is even. If it is, add it to a sum variable. Once the loop is finished, the sum variable will contain the total sum of all the even numbers in the range.

2. How do I add all the odd numbers in a given range in C++?

Similar to adding even numbers, you can use a for loop to iterate through the range and use an if statement to check if the current number is odd. If it is, add it to a sum variable. Once the loop is finished, the sum variable will contain the total sum of all the odd numbers in the range.

3. Is there a simpler way to add all the even or odd numbers in a given range in C++?

Yes, you can use the formula (n * (n + 1)) / 2, where n is the number of terms in the range. This formula works for both even and odd numbers and can be implemented in C++ with a simple function.

4. Can I add both even and odd numbers in the same loop in C++?

Yes, you can add both even and odd numbers in the same loop in C++. You can use the same loop structure as mentioned above and use an if-else statement to check if the current number is even or odd and add it to the appropriate sum variable.

5. How can I optimize my code for adding even and odd numbers in C++?

One way to optimize the code for adding even and odd numbers in C++ is to use a bitwise operator. For adding even numbers, you can use the bitwise AND (&) operator with the number 1 to check if the last bit is 0, indicating an even number. Similarly, for adding odd numbers, you can use the bitwise AND (&) operator with the number 1 to check if the last bit is 1, indicating an odd number. This method can be more efficient for larger ranges of numbers.

Similar threads

  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
752
  • Programming and Computer Science
Replies
3
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
7
Views
438
  • Programming and Computer Science
Replies
8
Views
867
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Precalculus Mathematics Homework Help
Replies
5
Views
778
Back
Top