C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

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




even and odd numbers

 

even and odd numbers, help with even and odd numbers

philip soraghan

9 Nov, 2008 - 04:17 PM
Post #1

New D.I.C Head
*

Joined: 3 Apr, 2008
Posts: 9

hi there i need help finding the sum of a set of even and odd integers in a set of integers like 4567 odds would be 12 evens would be 10 all i get is if i enter 4567 i get 4567 and 0
CODE

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int limit;
    int number;
    
    int sumeven=0;
    int sumodd=0;
    int count =0;
    cout<<"Enter limit of numbers "<<endl;
    cin>>limit;
    cout<<"Enter number"<<endl;
    cin>>number;


  
    if (number % 2 ==0)
{
    sumeven = count + number;
    count++;

  

}

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

  
    system("PAUSE");
    return EXIT_SUCCESS;
}


User is offlineProfile CardPM
+Quote Post


GWatt

RE: Even And Odd Numbers

9 Nov, 2008 - 04:31 PM
Post #2

//
Group Icon

Joined: 1 Dec, 2005
Posts: 2,496



Thanked: 61 times
Dream Kudos: 525
My Contributions
You need to break up the number into digits.
The easiest way to do this is read the characters into a string and then take the sum of the values of the digits
cpp

string num;
int even = 0, odd = 0;
cin >> num;
for (int i = 0; i < num.length(); i++)
{
if (num[i] - '0' % 2 == 0) //need to subtract '0' because we're dealing with ascii characters
even += num[i] - '0';
else
odd += num[i] - '0';

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 04:04AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month