Java BigInteger Calculate sumOfDigits(BigInteger n)

Here you can find the source of sumOfDigits(BigInteger n)

Description

sum Of Digits

License

Open Source License

Declaration

public static int sumOfDigits(BigInteger n) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.math.BigInteger;

public class Main {
    public static int sumOfDigits(long n) {
        int sum = 0;
        while (n != 0) {
            sum += n % 10;/*from  ww  w. j  av a 2s .  c  o  m*/
            n /= 10;
        }
        return sum;
    }

    public static int sumOfDigits(BigInteger n) {
        int sum = 0;
        while (!n.equals(BigInteger.ZERO)) {
            sum += n.remainder(BigInteger.TEN).intValue();
            n = n.divide(BigInteger.TEN);
        }
        return sum;
    }
}

Related

  1. subArray(BigInteger[] input, int start, int end)
  2. substring(final String lhs, final BigInteger _start, final BigInteger _end)
  3. sum(BigInteger valueA, BigInteger valueB)
  4. sum(BigInteger... values)
  5. sumOfDigits(BigInteger ab)
  6. trim(BigInteger bi)
  7. trim(BigInteger n)
  8. trimToPrecision(BigInteger number, int precision)
  9. uint64ToByteStreamLE(BigInteger val, OutputStream stream)