Display digit by digit for an integer value - C++ Data Structure

C++ examples for Data Structure:Algorithm

Description

Display digit by digit for an integer value

Demo Code

#include <iostream>

void separateInt(long);

int main(int argc, const char *argv[]) {
    long n;/*from   w  w w  .  j av  a2  s .  c o  m*/

    std::cout << "Enter multi digit integer: ";
    std::cin >> n;

    separateInt(n);

    return 0;
}
void separateInt(long n) {
    long numDigits = 1;  // number of tens places

    for (long i = n; i > 10; i /= 10) {
        numDigits *= 10;
    }

    while (numDigits > 0) {
        std::cout << (n / numDigits) % 10 << " ";

        numDigits /= 10;
    }
    std::cout << std::endl;
}

Result


Related Tutorials