How to resolve the algorithm Sum digits of an integer step by step in the Java programming language
How to resolve the algorithm Sum digits of an integer step by step in the Java programming language
Table of Contents
Problem Statement
Take a Natural Number in a given base and return the sum of its digits:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum digits of an integer step by step in the Java programming language
The provided code defines a class named SumDigits
that offers two static methods: sumDigits(long num)
and sumDigits(BigInteger num)
to calculate the sum of digits in a number represented as a long integer or a BigInteger. Additionally, there are two more static methods with the same names that accept an optional base parameter to perform the sum of digits in a specified base.
The sumDigits(long num)
method utilizes the overloaded method with an additional base
parameter, setting it to 10 by default. This method converts the long integer num
to its string representation in base 10 and iterates through its characters, adding their respective digits to the sum.
Similarly, the sumDigits(BigInteger num)
method also calls its overloaded version with the base
parameter, again defaulting to 10. It converts the BigInteger num
to its string representation in base 10 and proceeds to compute the sum of digits as with the long
version.
The overloaded methods sumDigits(long num, int base)
and sumDigits(BigInteger num, int base)
allow you to specify a custom base for calculating the sum of digits. They convert the input number to its string representation in the specified base and then iterate through its characters, adding their respective digits to the sum, taking into account the specified base.
The main
method demonstrates these methods by printing the sum of digits for a few sample numbers in various bases.
In summary, this code provides a versatile way to calculate the sum of digits in a number, whether represented as a long integer or a BigInteger, and supports specifying the base for the calculation.
Source code in the java programming language
import java.math.BigInteger;
public class SumDigits {
public static int sumDigits(long num) {
return sumDigits(num, 10);
}
public static int sumDigits(long num, int base) {
String s = Long.toString(num, base);
int result = 0;
for (int i = 0; i < s.length(); i++)
result += Character.digit(s.charAt(i), base);
return result;
}
public static int sumDigits(BigInteger num) {
return sumDigits(num, 10);
}
public static int sumDigits(BigInteger num, int base) {
String s = num.toString(base);
int result = 0;
for (int i = 0; i < s.length(); i++)
result += Character.digit(s.charAt(i), base);
return result;
}
public static void main(String[] args) {
System.out.println(sumDigits(1));
System.out.println(sumDigits(12345));
System.out.println(sumDigits(123045));
System.out.println(sumDigits(0xfe, 16));
System.out.println(sumDigits(0xf0e, 16));
System.out.println(sumDigits(new BigInteger("12345678901234567890")));
}
}
You may also check:How to resolve the algorithm Permutations step by step in the Perl programming language
You may also check:How to resolve the algorithm Animation step by step in the Scratch programming language
You may also check:How to resolve the algorithm Convert seconds to compound duration step by step in the 11l programming language
You may also check:How to resolve the algorithm Averages/Mean time of day step by step in the C++ programming language
You may also check:How to resolve the algorithm Next highest int from digits step by step in the Sidef programming language