Answers for "add all digits of a int together in jave"

4

add digits java

public static int sumDigits(int number){
        if (number < 10) return -1;
        int sum = 0;
        while (number > 10){
             sum += number % 10;
            number /= 10;
        }
        return sum + number;
Posted by: Guest on November-29-2021
0

add digits java

public static void main(String[] args) {
        int num = 321;
        int sum = 0;
        while (num > 0) {
            sum = sum + num % 10;
            num = num / 10;
        }
        System.out.println(sum); 
}
Posted by: Guest on March-02-2022

Code answers related to "add all digits of a int together in jave"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language