Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,052 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,562 people online right now. Registration is fast and FREE... Join Now!




sum of odds and evens

 
Reply to this topicStart new topic

sum of odds and evens

phil6326
4 Aug, 2008 - 04:26 PM
Post #1

New D.I.C Head
*

Joined: 30 Apr, 2008
Posts: 4

i am trying to add both the sum of the odd and even numbers of a set of numbers but when i run this program if i type in 3568 i get even numbers are 853800 and odds are 23459000 would appreciate any help


CODE
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    int number;
    int numint;
    int sumeven;
    int sumodd;
    int count =1;
    cout<<"Enter number of integers "<<endl;
    cin>> numint;
    cout<<"Enter number"<<endl;
    cin>>number;
  
  
  while ( count < number )
  {
    switch (number % 2)
        {
        case 0:
        sumeven = sumeven + count;
        count++;
        break;
        case 1:
        sumodd = sumodd + count;
        count++;
        break;
    }

  
}

    cout<<"The even numbers are "<<sumeven<<endl;
      cout<<"The odd  numbers are "<<sumodd<<endl;
      
    system("PAUSE");
    return EXIT_SUCCESS;
}


Mod Edit: added code tags: code.gif

This post has been edited by NickDMax: 4 Aug, 2008 - 04:35 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Sum Of Odds And Evens
4 Aug, 2008 - 04:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 212 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Well you had a few minor hiccups but this should help you out. Keep in mind that you need to initialize any variables before you attempt to use them in a calculation. You also tried using modulus on "number" when you should be testing "count".

cpp

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{

int number;
int numint;

// Initialize variables before using in equations.
int sumeven = 0;
int sumodd = 0;
int count =1;
cout<<"Enter number of integers "<<endl;
cin>> numint;
cout<<"Enter number"<<endl;
cin>>number;


// You are going to mod the counter not number
// counter represents the current count
while ( count < number )
{
switch (count % 2)
{
case 0:
sumeven = sumeven + count;
break;
case 1:
sumodd = sumodd + count;
break;
}

// Increment count here, since both cases need to increment
// cut it out and place it at the end of the loop
count++;

}

cout<<"The even numbers are "<<sumeven<<endl;
cout<<"The odd numbers are "<<sumodd<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}


Read the in-code comments to see how this is done using your code. Also keep in mind that since count < number it will not count the last number in the sum. You have to do <= if you want to include the last number.

So for instance if you type in 10, it will read even as 20 and not even as 30 because it didn't add in "10".

Enjoy!

"At DIC we be integer summing code ninjas... we also "summon" things like giant marshmallow men to destroy NYC!" decap.gif
User is online!Profile CardPM
+Quote Post

penguin2
RE: Sum Of Odds And Evens
4 Aug, 2008 - 04:42 PM
Post #3

D.I.C Head
Group Icon

Joined: 22 Jul, 2008
Posts: 79



Thanked: 1 times
Dream Kudos: 25
My Contributions
I don't understand exactly what you are trying to do. Do you want to put in 3568 and the sum of the odd digits and the sum of the even digits? Of do you want to put in a list of numbers, sort it into even and odd numbers, and then get the sum of the even numbers and the sum of the odds?

Why do you prompt for numint if you never use it?

If I were getting a list of nubmers and get the sum of evens and odds, I would prompt for numint, initialize an array with numint-1 elements, use a for loop to prompt for a value for each element, and use another for loop to determine whether each element is odd or even, and add it to the appropriate value.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 05:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month