Read a five-digit integer, separates the integer into its digits and prints them separated by three spaces each. - C++ Language Basics

C++ examples for Language Basics:Console

Description

Read a five-digit integer, separates the integer into its digits and prints them separated by three spaces each.

Demo Code

#include <iostream>
#include <string>

void printDigits(int);

int main(int argc, const char *argv[]) {
    int num;//from   w w  w. ja  va 2s.c o m

    std::cout << "Enter a five digit integer: ";
    std::cin >> num;

    printDigits(num);

    std::cout << std::endl;

    return 0;
}

void printDigits(int num) {
    const std::string SPACES = "   ";
    if (num / 10 > 0)
        printDigits(num / 10);

    std::cout << num % 10 << SPACES;
}

Result


Related Tutorials