Hi all
I've been asked to write a C++ program that the user enters a five digit integer.
The program then needs to return how many zeros in the number, how many odd numbers and how many even numbers.
I've set it up to %2=0 and %2!=0 to determine if the number is odd or even.
I don't know how to separate the odds and evens.
Any help would be great.
Thanks
Ian

You can set up a while loop which will iterate 5 times.
Each time you will extract the last digit by: d=n%10 ,now you can check whether it is 0 or odd or even and then divide the number by 10(n=n/10) which will remove the last digit so each time you will get a different last digit.

the pseudo code is

*

int copy=5digitnumber
while(copy>0)
{
    d=copy%10;
    check whether d==0 {zero++} or d%2==0{even++} or d%2!=0{odd++}. 
    copy=copy/10;
}   

*

commented: good answer :) +14

There is an easy mathematical way to do it, but in C++ one should write the program like this:

#include <iostream>
#include <string>
#include <vector>
#include <boost/lexical_cast.hpp>

using namespace std;

const int MAX_SIZE = 5;

int main(vector<string> args) {
    try {
        int num;
        cin >> num;

        string str_num = boost::lexical_cast<string>(num);
        int values[MAX_SIZE];

        if(str_num.size() != 5) throw string("incorrect size of an integer, should be 5, but it is " +
                                              boost::lexical_cast<string>(str_num.size()));

        int i = 0;

        for(char c: str_num) {
            values[i] = boost::lexical_cast<int>(c);
            i++;
        }

        int even = 0, odd = 0, zeroes = 0;

        for(int t : values) {
            if(t == 0)  zeroes++;

            if(t % 2) odd++;
            else even++;
        }

        cout << "\nnumber of even is " << even
             << "\number of odd is " << odd
             << "\nnumber zeroes is " << zeroes;
    } catch(string t) {
        cout << "exception has occured:\n " << t;
    }
}

Note that to compile this, you have to take out the vector<string> args from implementation of main. And your professor will probably not take this code ;)
Note that yo can convert integers to strings through sstream. But if you are not an evil C nazi (you get it: SS tream??), you will of course you boost.

Your program looks nice but your use of boost library is probably not allowed in his program. It's like hitting a nail with a sludge hammer, way too overkill.

commented: If I don't use at least one function/class from boost (at least STL) in 50 lines of code I may explode +0

My mistake ancient dragon. This version is without sludge hammer:

#include <stdio.h> 
#include <stdlib.h>

#define MAX_SIZE 5

#define false 1
#define true 0
int i;

#define ODD(X) (X%2)

main(nargs, args)
int nargs;
char **args;
 {

    int *num = (int*)malloc(sizeof(int));
    *num = 0;

    printf("enter number: ");
    if(!(scanf("%i", num) != -1) || (*num/10000 < 1 || *num/10000 > 10)) return 0xDEADBEEF;


    int values[MAX_SIZE];

    int*odd=(int*)malloc(sizeof(int)),*even=(int*)malloc(sizeof(int)),*zeroes=(int*)malloc(sizeof(int)),*remains=(int*)malloc(sizeof(int));
    *odd=*even=*zeroes=*remains=true;
    *remains = *num;

    for(i = 0; i < MAX_SIZE; i++) {
        values[i] = *remains%10;
        *remains /= 10;
    }

    for (i = true; i < MAX_SIZE; i++) {
        if(ODD(values[i])) (*odd)++;
        else (*even)++;
        if (!values[i]) (*zeroes)++;
    }
    printf("even: %d\nodd: %d\nzeroes: %d", *even, *odd, *zeroes);
}

(is 0 odd or even?)

Are you kidding me??!!??

Forst of all, we don't write programs for people, we help them write their own.

And the code:

#define ODD(X) (X%2)        // Why? This is just confusing when used

main(nargs, args)           // I haven't seen this MAIN signature since
int nargs;                  //    1985! AFAIK, this has been deprecated 
char **args;                //    for over 30 years
 {

    int *num = (int*)malloc(sizeof(int));   // Malloc one int? You're nuts!
    *num = 0;

    printf("enter number: ");
    if(!(scanf("%i", num) != -1) || (*num/10000 < 1 || *num/10000 > 10)) return 0xDEADBEEF;
                            // Unreadable worthless combination of multiple statements

    int values[MAX_SIZE];

    int*odd=(int*)malloc(sizeof(int)),*even=(int*)malloc(sizeof(int)),*zeroes=(int*)malloc(sizeof(int)),*remains=(int*)malloc(sizeof(int));
                            // Another unreadable mess, again mallocing single integers

That's enough...
Ian, don't bother with this code, there's nothing to learn from it. You can write your code far simpler than this.

Profyou gave helpful hints you can actually use.

you all are making things complex. Here's the simplest way of doing

int num;
printf("enter any five digit num \n");
scanf("%d",&num);
len=findoutlength(num);
while(len>0)
{
  d=num%10;
  if(d==0)
  {
  printf("digit %d is 0",d);
  }
  else if(d%2==0)
  {
  printf("digit %d is even",d);
  }
  else
  {
  printf("digit %d is odd",d);
  }
  len--;
}  

I guess this will give you the desired output.

phani1092, what part of "First of all, we don't write programs for people, we help them write their own" did you not understand.

main(nargs, args)           // I haven't seen this MAIN signature since
int nargs;                  //    1985! AFAIK, this has been deprecated 
char **args;                //    for over 30 years
 {

Would you believe some people still write code in that style? I've seen in recently (10 years or so) in some libraries, which were probably started 25+ years ago.

Would you believe

Unfortunately, I do believe it. Lucky for some programmers deprecated only means "you shouldn't use it" rather than "don't"

Unfortunately every one, this code is first semester first year and i can only use what I've learn so far. I thought there might be an if or a for or a while statement I could use.
Thanks

You probably could but ModernC++ completely derailed your thread proving his prowess at programming but not his aptitude for aiding amateurs.

Start a new thread after reading Read This Before Posting A Question. Then we can get back on track.

Do you have problem understanding the pseudo code i gave you earlier?
Thats the way a simple problem can be solved in a simple problem.Moreover that while loop can be used to solve a variety of similar problems...
i am reposting that:
You should try to complete it.

int copy=5digitnumber
while(copy>0)
{
    d=copy%10;
    check whether d==0 {zero++} or d%2==0{even++} or d%2!=0{odd++}. 
    copy=copy/10;
}   
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.