Finding the sum of the digits

2020-04-10 03:52发布

I have a 5-digit integer, say

int num = 23456;

How to find the sum of its digits?

标签: c algorithm math
9条回答
我想做一个坏孩纸
2楼-- · 2020-04-10 04:33
   #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 the sprintf and the ascii number representation

查看更多
甜甜的少女心
3楼-- · 2020-04-10 04:38
#include <stdio.h>

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

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

    printf("%i", sum);

    return 0;
}
查看更多
Summer. ? 凉城
4楼-- · 2020-04-10 04:41

How about this:

for(sum=0 ,num=23456;num; sum+=num %10, num/=10);
查看更多
成全新的幸福
5楼-- · 2020-04-10 04:52

Actually, I answered with another (somewhat humorous) answer which used a absolutely huge array for a table lookup but, thinking back on it, it's not that bad an idea, provided you limit the table size.

The following functions trade off space for time. As with all optimisations, you should profile them yourself in the target environment.

First the (elegant) recursive version:

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];
}

It basically separates the number into three-digit groupings with fixed lookups for each possibility. This can easily handle a 64-bit unsigned value with a recursion depth of seven stack frames.

For those who don't even trust that small amount of recursion (and you should, since normal programs go that deep and more even without recursion), you could try the iterative solution:

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;
}

These are probably three times faster than the one-digit-at-a-time solution, at the cost of a thousand bytes of data. If you're not adverse to using 10K or 100K, you could increase the speed to four or five times but you may want to write a program to generate the static array statement above :-)

As with all optimisation options, measure, don't guess!

I prefer the more elegant recursive solution myself but I'm also one of those types who prefer cryptic crosswords. Read into that what you will.

查看更多
祖国的老花朵
6楼-- · 2020-04-10 04:54

If you want a way to do it without control statements, and incredibly efficient to boot, O(1) instead of the O(n), n = digit count method:

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];
}

:-)

查看更多
手持菜刀,她持情操
7楼-- · 2020-04-10 04:55
#include<stdio.h>
main()
{
                 int sum=0,n;
                 scanf("%d",&n);
                 while(n){
                       sum+=n%10;
                       n/=10;
                 }
                 printf("result=%d",sum);
}

sum is the sum of digits of number n

查看更多
登录 后发表回答