#include <iostream>
usingnamespace std;
void digitCount(long num);
int main ()
{
long num;
cout <<"Enter a long number. " <<endl;
cin >>num;
cout <<endl;
digitCount(num);
system("PAUSE");
return 0;
}
void digitCount(long num)
{
int e = 0, z = 0, o = 0, x = 0;
for (int i = 0; i <= 11; i++)
{
x = num % 10;
if (x % 10 == 0)
z++;
if (x % 10 != 0)
{
if ((x % 2) == 0)
e++;
elseif ((x % 2) != 0)
o++;
}
num = num / 10;
}
cout << "number of zeros = " << z << endl;
cout << "number of odd numbers = " << o << endl;
cout << "number of even numbers = " << e << endl;
}
My output is:
number of zeros = 2
number of odd numbers = 6
number of even numbers = 4
The odd numbers should be 5 given the number I put in, and that same output comes up no matter what long number I put in. I need this soon...
A long number like 23040598763 does not fit into a 32 bit integer. Try using longlong as the variable type or testing with numbers smaller than 2^32=4294967296
One more thing I should mention: You are working with signed integers so really you should be using numbers that are less than 2^31. Since one bit is dedicated to the sign of the number.
Does the negative sign on a 10 digit negative number count as 11 digits? If not, I am afraid you are out of luck getting an 11 digit number from a 32 bit integer.
I'm not allowed to use long long for this program.
But I have to use an 11 digit number!
I've seen this pattern before. Every suggested solution is shot down as being forbidden. It might be better to post the full question, including any restrictions, at the start.
But having said that a couple of other thoughts come to mind. One is the use of a string, the other is the use of the double type.