C - Issue converting a user generated number into words

后端 未结 3 2039
予麋鹿
予麋鹿 2021-01-15 16:56

So I\'ve been working my way through Kochan\'s Programming in C and I\'ve hit a snag on one of the questions which reads as follows:

\"Write a program that takes an

3条回答
  •  深忆病人
    2021-01-15 17:36

    My simple answer:

    void printNum(int x)
    {
        static const char * const num[] = {
            "zero ", "one ", "two "  , "three ", "four ",
            "five ", "six ", "seven ", "eight ", "nine "
        };
    
        if (x < 10) {
            printf(num[x]);
            return;
        }
        printNum(x / 10);
        printNum(x % 10);
    }
    

提交回复
热议问题