0

I want to get the decimal digits separately when I get the number. For example, if I get 123 or 321, I want to sort the array or print the digits "1 2 3" or "3 2 1" in C.

Would you please give me some any advice? Use C grammar?

int nums;
scanf("%d", &nums) // imagin this nums is 123

// and how can I get the number 1,2,3? 

In the array. I will sort the number like

for(int i = 0; i<3; i++)
 array[i] = nums;

and I expect in the array number is probably {1,2,3};

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Pilfer Ahn
  • 91
  • 1
  • 10
  • 1
    How do you do it without a computer? There are a couple of answers to that, of course. One is that you look at the string of decimal digits and then take each digit in turn. You can use `sprintf()` to format a number into a string. Or you use division and remainders — you can use that technique too. Your choice. Both work. – Jonathan Leffler Jun 08 '19 at 07:52
  • related/dupe? https://stackoverflow.com/questions/41797642/reverse-number-function-in-c – Jean-François Fabre Jun 08 '19 at 07:55
  • dupe https://stackoverflow.com/questions/9302681/c-how-to-break-apart-a-multi-digit-number-into-separate-variables – Finomnis Jun 08 '19 at 17:18
  • Possible duplicate of [C: how to break apart a multi digit number into separate variables?](https://stackoverflow.com/questions/9302681/c-how-to-break-apart-a-multi-digit-number-into-separate-variables) – Finomnis Jun 08 '19 at 17:19
  • 1
    Possible duplicate of [Identify the digits in a given number.](https://stackoverflow.com/questions/981162/identify-the-digits-in-a-given-number) – outis Jun 09 '19 at 19:49
  • ... [Parsing number into digits with scanf()](https://stackoverflow.com/q/22987166/90527) – outis Jun 09 '19 at 19:59

3 Answers3

2

You can use a recursive function printing the modulo in each call:

#include <stdio.h>

static void print(int value)
{
    if (value != 0) {
        print(value / 10);
        printf("%d, ", value % 10);
    }
}

int main(void) 
{
    int value;

    scanf("%d", &value);
    print(value);
    return 0; 
}

input: 123

output: 1, 2, 3,

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
  • 1
    Better write `if (value != 0) {` to make clear what you mean. ;-) Even if C handles int like bool they are different concepts. – the busybee Jun 08 '19 at 08:28
  • @thbusybee disagreed. This is the way C programmers use it – 0___________ Jun 08 '19 at 08:58
  • @P__J__ I disagree. In this case I would definitely use `!= 0` since `value` is not used as a Boolean. This is better. – klutt Jun 08 '19 at 09:17
  • 1
    You shouldn't use recursion if you don't need to. A simple while loop would do the trick. – Cheatah Jun 08 '19 at 11:40
  • @Cheatah IMO recursion is not so bad when you know that you can not overflow the stack (like in this case where the worst case is 10 iterations -> 40 bytes) – David Ranieri Jun 09 '19 at 07:24
0

I have an application that uses a mp3 player to announce a number. My “SayNum” function takes a (positive) integer like 123 and say it verbally like “One” … “Two” … “Three”. This is the function >> (simplified , left out the detail of commands to the mp3 player to play the tracks):

void SayNum (int Num) // Say- Number- Function
{
    uint8_t Digit;
    int DivNum = 1;
    while ((DivNum * 10) <= Num) DivNum *= 10;
    while (DivNum >= 1)
    {
        Digit = (Num / DivNum) % 10;
        // Say the digit (play mp3 audio file) >>
        DivNum /= 10;
    }
}

You could easily add an index counter in the while loop and then use it to store each digit in an array.

Johan
  • 239
  • 2
  • 8
0

Version 2 of SayNum: The “% 10”- method works ok if you’re working with positive integers only. With real numbers (floats), things get a bit tricky, so I used a new method based on “sprintf”- function.

void SayNum (double Num, uint8_t Dp)  // Say Number Ver 2
{
    uint8_t Digit;
    char NumStr[20];

    sprintf (NumStr,"%1.*f",Dp,Num);

    for (int x = 0; x < strlen(NumStr); x++)
    {
        switch (NumStr[x])
        {
            case '-':
                // Say "Minus" (play mp3 audio file) >>
            break;

            case '.':
                // Say "Point" (play mp3 audio file) >>
            break;

            case '0' ... '9':
                Digit = (int)(NumStr[x]) - 48;
                // Say the Digit (play mp3 audio file) >>
            break;
        }
    }
}

With this function you can do positive or negative integers or real numbers. You can also specify how many numbers after the decimal point you want.

Example1 : SayNum(-12,0); // Says the integer -12 (with no decimal places)

Example2 : SayNum(12.34,2); // Says the real number 12.34 (with 2 decimal places)

Johan
  • 239
  • 2
  • 8