Sum of digits in C#

前端 未结 18 1967
耶瑟儿~
耶瑟儿~ 2020-11-28 07:21

What\'s the fastest and easiest to read implementation of calculating the sum of digits?

I.e. Given the number: 17463 = 1 + 7 + 4 + 6 + 3 = 21

相关标签:
18条回答
  • 2020-11-28 07:40

    I would suggest that the easiest to read implementation would be something like:

    public int sum(int number)
    {
        int ret = 0;
        foreach (char c in Math.Abs(number).ToString())
            ret += c - '0';
        return ret;
    }
    

    This works, and is quite easy to read. BTW: Convert.ToInt32('3') gives 51, not 3. Convert.ToInt32('3' - '0') gives 3.

    I would assume that the fastest implementation is Greg Hewgill's arithmetric solution.

    0 讨论(0)
  • 2020-11-28 07:42
    int n = 17463; int sum = 0;
    for (int i = n; i > 0; i = i / 10)
    {
    sum = sum + i % 10;
    }
    Console.WriteLine(sum);
    Console.ReadLine();
    
    0 讨论(0)
  • 2020-11-28 07:43

    I thought I'd just post this for completion's sake:

    If you need a recursive sum of digits, e.g: 17463 -> 1 + 7 + 4 + 6 + 3 = 21 -> 2 + 1 = 3
    then the best solution would be

    int result = input % 9;
    return (result == 0 && input > 0) ? 9 : result;
    
    0 讨论(0)
  • 2020-11-28 07:43

    If one wants to perform specific operations like add odd numbers/even numbers only, add numbers with odd index/even index only, then following code suits best. In this example, I have added odd numbers from the input number.

    using System;
                        
    public class Program
    {
        public static void Main()
        {
            Console.WriteLine("Please Input number");
            Console.WriteLine(GetSum(Console.ReadLine()));
        }
        
        public static int GetSum(string num){
            int summ = 0;
            for(int i=0; i < num.Length; i++){
                int currentNum;
                if(int.TryParse(num[i].ToString(),out currentNum)){
                     if(currentNum % 2 == 1){
                        summ += currentNum;
                    }
                }
           } 
           return summ;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 07:48
    #include <stdio.h>
    
    int main (void) {
    
        int sum = 0;
        int n;
        printf("Enter ir num ");
        scanf("%i", &n);
    
        while (n > 0) {
            sum += n % 10;
            n /= 10;
        }
    
        printf("Sum of digits is %i\n", sum);
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-28 07:49

    A while back, I had to find the digit sum of something. I used Muhammad Hasan Khan's code, however it kept returning the right number as a recurring decimal, i.e. when the digit sum was 4, i'd get 4.44444444444444 etc. Hence I edited it, getting the digit sum correct each time with this code:

     double a, n, sumD;
     for (n = a; n > 0; sumD += n % 10, n /= 10);
     int sumI = (int)Math.Floor(sumD);
    

    where a is the number whose digit sum you want, n is a double used for this process, sumD is the digit sum in double and sumI is the digit sum in integer, so the correct digit sum.

    0 讨论(0)
提交回复
热议问题