Hi all,
I'm trying to write a program that inputs an integer with multiple digits and outputs the number of odd, even, and zero digits in the original number.
I think I have most of it figured out, but I don't know how to make it read and evaluate individual digits. I assume I need to use a char variable somewhere...? Here's what I have so far:
#include<iostream>
usingnamespace std;
int main ()
{
long num = 0;
char ch;
int len = 0;
int i = 0;
int odd = 0;
int even = 0;
int zero = 0;
cout << "How many digits are in your number? ";
cin >> len;
cout << "Please enter a number with " << len << " digits: ";
cin >> num;
for (i=1; i<=len; i++)
{
//Here's where I don't know what to put, but I assume each
//digit will be the variable ch of type char?
if (ch == 0)
zero++;
elseif (ch % 2 == 0)
even++;
else
odd++;
}
cout << "The number of even digits is: " << even << endl;
cout << "The number of odd digits is: " << odd << endl;
cout << "The number of zero digits is: " << zero << endl;
system("pause");
return 0;
}
Also, is there a way to omit the input of the number of digits and just have the program tell how long the number is?
Sorry, I'm still confused. I looked up the modulus and division operators and I think I understand how they work, but how do I use them to isolate each digit of the number to test if each digit is even, odd, or zero?