Getting each individual digit from a whole integer

后端 未结 9 1271
南方客
南方客 2020-11-30 04:05

Let\'s say I have an integer called \'score\', that looks like this:

int score = 1529587;

Now what I want to do is get each digit 1, 5, 2,

9条回答
  •  醉话见心
    2020-11-30 04:25

    I've made this solution, it-s simple instead read an integer, i read a string (char array in C), then write with a for bucle, the code also write the sum of digits

    // #include
    
    scanf("%s", n);
    int total = 0;
    
    for (int i = 0; i< strlen(n); i++){
        printf("%c", n[i]);
        total += (int)(n[i]) -48;
    }
    
    printf("%d", total);
    

提交回复
热议问题