Digit amount search

I have a 5 digit integer, say

int num = 23456;

How to find the sum of your numbers?

+3
source share
9 answers

Use modulo operation to get the value of the least significant digit:

int num = 23456;
int total = 0;
while (num != 0) {
    total += num % 10;
    num /= 10;
}

If the input can be a negative number, then it would be nice to check this and invert the sign.

+14
source
#include <stdio.h>

int main()
{
    int i = 23456;
    int sum = 0;

    while(i)
    {
        sum += i % 10;
        i /= 10;
    }

    printf("%i", sum);

    return 0;
}
+4
source
int sum=0;while(num){sum+=num%10;num/=10;}

, , C99 .

?

+3

:

for(sum=0 ,num=23456;num; sum+=num %10, num/=10);
+2

, O(1) O(n), n = digit count:

int getSum (unsigned int val) {
    static int lookup[] = {
         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, //     0-    9
         1,  2,  3,  4,  5,  6,  7,  8,  9, 10, //    10-   19
         2,  3,  4,  5,  6,  7,  8,  9, 10, 11, //    20-   29
         :
         9, 10, 11, 12, 13, 14, 15, 16, 17, 18, //    90-   99
         :
        14, 15, 16, 17, 18, 19, 20, 21, 22, 23, // 23450-23459
        ::
    };
    return lookup[23456];
}

: -)

+2

: , :

if (num%3==0) return (num%9==0) ? 9 : 3;

.

+1

, ( ) , , , , , .

. , .

() :

unsigned int getSum (unsigned int val) {
    static const unsigned char lookup[] = {
         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, //   0-  9
         1,  2,  3,  4,  5,  6,  7,  8,  9, 10, //  10- 19
         2,  3,  4,  5,  6,  7,  8,  9, 10, 11, //  20- 29
         :
        18, 19, 20, 21, 22, 23, 24, 25, 26, 27  // 990-999
    };
    return (val == 0) ? 0 : getSum (val / 1000) + lookup[val%1000];
}

. 64- .

, ( , ), :

unsigned int getSum (unsigned int val) {
    static const unsigned char lookup[] = {
         0,  1,  2,  3,  4,  5,  6,  7,  8,  9, //   0-  9
         1,  2,  3,  4,  5,  6,  7,  8,  9, 10, //  10- 19
         2,  3,  4,  5,  6,  7,  8,  9, 10, 11, //  20- 29
         :
        18, 19, 20, 21, 22, 23, 24, 25, 26, 27  // 990-999
    };
    unsigned int tot = 0;
    while (val != 0) {
        tot += lookup[val%1000];
        val /= 1000;
    }
    return tot;
}

, , , , . 10K 100K, , : -)

, , !

I prefer a more elegant recursive solution myself, but I am also one of those types who prefer mysterious crosswords. Read what you will.

0
source
   #include <stdio.h>
  2 #include <stdlib.h>
  3
  4 #define BUFSIZE 20
  5
  6 int main(void)
  7 {
  8         int     number = 23456;
  9         char    myBuf[BUFSIZE];
 10         int     result;
 11         int     i = 0;
 12
 13         sprintf(myBuf,"%i\0",number);
 14
 15         for( i = 0; i < BUFSIZE && myBuf[i] != '\0';i++)
 16         {
 17                 result += (myBuf[i]-48);
 18         }
 19
 20         printf("The result is %d",result);
 21         return 0;
 22 }
 23

Another idea here using sprintf and the representation of the ascii number

0
source
#include<stdio.h>
main()
{
                 int sum=0,n;
                 scanf("%d",&n);
                 while(n){
                       sum+=n%10;
                       n/=10;
                 }
                 printf("result=%d",sum);
}

sum - sum of digits of n

0
source

Source: https://habr.com/ru/post/1759824/


All Articles