Getting each individual digit from a whole integer

后端 未结 9 1177
南方客
南方客 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<string.h>
    
    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);
    
    0 讨论(0)
  • 2020-11-30 04:26

    This solution gives correct results over the entire range [0,UINT_MAX] without requiring digits to be buffered.

    It also works for wider types or signed types (with positive values) with appropriate type changes.

    This kind of approach is particularly useful on tiny environments (e.g. Arduino bootloader) because it doesn't end up pulling in all the printf() bloat (when printf() isn't used for demo output) and uses very little RAM. You can get a look at value just by blinking a single led :)

    #include <limits.h>
    #include <stdio.h>
    
    int
    main (void)
    {
      unsigned int score = 42;   // Works for score in [0, UINT_MAX]
    
      printf ("score via printf:     %u\n", score);   // For validation
    
      printf ("score digit by digit: ");
      unsigned int div = 1;
      unsigned int digit_count = 1;
      while ( div <= score / 10 ) {
        digit_count++;
        div *= 10;
      }
      while ( digit_count > 0 ) {
        printf ("%d", score / div);
        score %= div;
        div /= 10;
        digit_count--;
      }
      printf ("\n");
    
      return 0;
    }
    
    0 讨论(0)
  • 2020-11-30 04:28

    RGB values fall nicely on bit boundaries; decimal digits don't. I don't think there's an easy way to do this using bitwise operators at all. You'd need to use decimal operators like modulo 10 (% 10).

    0 讨论(0)
  • 2020-11-30 04:31

    Don't reinvent the wheel. C has sprintf for a reason. Since your variable is called score, I'm guessing this is for a game where you're planning to use the individual digits of the score to display the numeral glyphs as images. In this case, sprintf has convenient format modifiers that will let you zero-pad, space-pad, etc. the score to a fixed width, which you may want to use.

    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-11-30 04:37
    #include<stdio.h>
    
    int main() {
    int num; //given integer
    int reminder;
    int rev=0; //To reverse the given integer
    int count=1;
    
    printf("Enter the integer:");
    scanf("%i",&num);
    
    /*First while loop will reverse the number*/
    while(num!=0)
    {
        reminder=num%10;
        rev=rev*10+reminder;
        num/=10;
    }
    /*Second while loop will give the number from left to right*/
    while(rev!=0)
    {
        reminder=rev%10;
        printf("The %d digit is %d\n",count, reminder);
        rev/=10;
        count++; //to give the number from left to right 
    }
    return (EXIT_SUCCESS);}
    
    0 讨论(0)
提交回复
热议问题