Search code examples
c++algorithmpseudocode

Separating number input into units


I need to separate users input into units and store them in an array. e.g if user enters 6547. Array will store {6,5,4,7} Using C++ on Linux

I would appreciate if you can help me with pseudocode or explain an algorithm.

I'm a beginner so please restrain from advising advanced function (and explain its use if you do) as we have studied basics so far

N.B| If such question has already been answered and I skipped it in search, please do point me to it.


Solution

  • The math for isolating right most digit:

    digit = digit % 10;  
    

    The math for shifting a number right by one digit:

    new_number = old_number / 10;  
    

    Every letter and number can be represented as a text character. For example, '5' is a character representing the single decimal digit 5.

    The math for converting a textual digit (character) to numeric:

    digit = char_digit - '0';
    

    Example:

      digit = '9' - '0';
    

    The math for converting a numeric digit to a textual digit (character):

    char_digit = digit + '0';
    

    Example:

      char_digit = 5 + '0';