Identify the digits in a given number.

前端 未结 7 733
滥情空心
滥情空心 2020-12-07 03:41

I\'m new to programming, and I\'m stuck at a problem. I want my program to identify the separate digits in a given number, like if I input 4692, it should ident

7条回答
  •  渐次进展
    2020-12-07 04:03

    Here is a simple solution if you want to just print the digits from the number.

    #include 
    /**
    printdigits
    */
    void printDigits(int num) {
    
       char buff[128] = "";
       sprintf(buff, "%d ", num);
       int i = 0;
       while (buff[i] != '\0') {
          printf("%c ", buff[i]);
          i++;
       }
       printf("\n");
    }
    /*
    main function
    */
    int main(int argc, char** argv) {
       int digits = 4321;
       printDigits(digits);
       return 0;
    }
    

提交回复
热议问题