Java Program to Calculate the Sum of Digits of an Integer

sum of digits

Java programs to calculate the sum of the digits of an integer have been shown here. For example if a number is 12573 then the sum of the digits is (1 + 2 + 5 + 7 + 3) i.e. 18. The following section covers the iterative approach to find the sum of digits. The algorithm, time complexity and pseudocode of the program have been shown below.






1. Algorithm to calculate sum of digits of an integer


1. Take a number x as input.

2. Initialize a variable to 0 say sum = 0.

3. Perform r = x % 10 and add r to sum i.e sum = sum + r

4. Perform x = x / 10

5. If x = 0 stop the process and display sum as output else go to step 3.




2. Pseudocode to calculate sum of digits of an integer


Input : A number $n$

Output : The sum of digits of $n$

1. Procedure sumOfDigits($n$):

2. $sum \leftarrow 0$

3. Repeat until $n \neq 0$

4. $sum \leftarrow sum + (n \mod 10)$

5. $n \leftarrow n / 10$

6. Return $sum$

7. End Procedure





3. Time complexity to calculate sum of digits of an integer


Time Complexity: O(log(n))

Where n is the input number.





4. Program & output to calculate sum of digits of an integer




4.1. Java Program & output to calculate sum of digits of an integer using iteration

Code has been copied
/*************************************
           alphabetacoder.com
 Java program to calculate the sum of 
 digits of an integer using iteration
***************************************/

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare variables
        int num, sum = 0, r;

        // take inputs
        System.out.print("Enter the number = ");
        num = sc.nextInt();

        //find sum of the digits
        while (num != 0) {
            // extract the unit digit
            r = num % 10;
            // add r to sum
            sum = sum + r;
            //divide the number by 10
            num = num / 10;
        }

        // display result
        System.out.print("Sum of digits = " + sum);
    }
}

Output


Enter the integer = 5512

Sum of digits = 13




4.2. Java Program & output to calculate sum of digits of an integer using recursion

Code has been copied
/*************************************
           alphabetacoder.com
 Java program to calculate the sum of 
 digits of an integer using recursion
**************************************/

import java.util.Scanner;

class Main {
    // recursive function to calculate sum of digits
    int digit_sum(int num) {
        // base condition
        if (num == 0)
            return 0;
        else // recursive condition
            return (num % 10) + digit_sum(num / 10);
    }

    public static void main(String[] args) {
        // declare instance of Scanner class
        Scanner sc = new Scanner(System.in);

        // declare object of Main class
        Main obj = new Main();

        // declare variables
        int num;

        // take inputs
        System.out.print("Enter the number = ");
        num = sc.nextInt();

        // calculate the sum by calling function
        // display result 
        System.out.print("Sum of digits = " + obj.digit_sum(num));
    }
}

Output


Enter the integer = 1234

Sum of digits = 10