Getting each individual digit from a whole integer

后端 未结 9 1272
南方客
南方客 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:35

    //this can be easily understandable for beginners     
    int score=12344534;
    int div;
    for (div = 1; div <= score; div *= 10)
    {
    
    }
    /*for (div = 1; div <= score; div *= 10); for loop with semicolon or empty body is same*/
    while(score>0)
    {
        div /= 10;
        printf("%d\n`enter code here`", score / div);
        score %= div;
    }
    

提交回复
热议问题