### even and odd digits in an integer

could someone tell me how can I separate digits from an integer and determine if the digits are even, odd or zero?
Last edited on
personally I would convert to a string and check each to see if they are 1, 3, 5, 7, or 9, else they are even.
1. Convert to a string
2.Loop through string
2a. Convert this character to string
2b. Convert that string to an integer ( std::atoi )
2c. If int % 2 == 0 -> even character
 ``12345678910111213141516171819`` ``````#include int main() { unsigned int number = 70489026 ; do { const int last_digit = number % 10 ; std::cout << last_digit << ' ' ; if( last_digit == 0 ) std::cout << "zero\n" ; else if( last_digit%2 == 0 ) std::cout << "even\n" ; else std::cout << "odd\n" ; number /= 10 ; } while( number != 0 ) ; }``````
 ``123456789101112131415`` ``````#include using namespace std; void classify( int n ) { if ( !n ) return; classify( n / 10 ); int d = n % 10; cout << d << ": " << ( d == 0 ? "Zero" : ( d % 2 ? "Odd" : "Even (and not zero)" ) ) << endl; } int main() { classify( 12345600 ); }``````
Topic archived. No new replies allowed.