Sum of Digits of a Number in Java

In this section, we will create Java programs to find the sum of digits of a number in Java. In order to find the sum of digits of a number, we must familiar with the Java loops and operators.

Sum of Digits of a Number in Java

Steps to Find the Sum of Digits of a Number in Java

Enter any integer number as input. After that, we use modulus and division operation respectively to find the sum of digits of the number as output. Let's see the steps.

  1. Read or initialize an integer N.
  2. Declare a variable (sum) to store the sum of numbers and initialize it to 0.
  3. Find the remainder by using the modulo (%) operator. It gives the last digit of the number (N).
  4. Add the last digit to the variable sum.
  5. Divide the number (N) by 10. It removes the last digit of the number.
  6. Repeat the above steps (3 to 5) until the number (N) becomes 0.

Let's understand the above steps mathematically and find the sum of digits of a number.

Suppose, we have to find the sum of digits of the number (N) 674.

First iteration

674 % 10 = 4

Sum = 0 + 4 = 4

674 / 10 = 67

Second iteration

67 % 10 = 7

Sum = 4 + 7 = 11

67 / 10 = 6

Third iteration

6 % 10 = 6

Sum = 11 + 6 = 17

6 / 10 = 0

Now the number (N) has become 0. So, we will not process it further. Hence, we get 17 as the sum of digits of the number 674.

Let's implement the above steps in a Java program.

Java Programs to Find the Sum of Digits of a Number

There are the following ways to find the sum of digits of a number:

  • Using While Loop
  • Using for Loop
  • Using Function
  • Using Recursion

Using While Loop

It is the simplest approach to find the sum of the digits of a number using a while loop. Let's break down the steps:

Initialize Variables:

number: It stores the user input.

digit: It holds the last digit of the number.

sum: It accumulate the sum of the digits.

Loop Until the Number Becomes 0:

  • Find the last digit using the modulo operator (number % 10).
  • Add the last digit to sum.
  • Remove the last digit from number by dividing it by 10 (number / 10).

Print the Result.

SumOfDigitExample1.java

Output:

Enter the number: 876
Sum of Digits: 21

Using for Loop

This approach uses a for loop to achieve the same result, which might be more concise in some cases.

Initialize Variables:

number: It stores the user input.

sum: It accumulates the sum of the digits.

Loop Until the Number Becomes 0:

In each iteration, add the last digit to sum and remove the last digit from number.

Print the Result.

SumOfDigitsExample2.java

Output:

Enter a number: 3456788
Sum of digits: 41

Using Function

Encapsulating the logic in a function improves code reusability and clarity.

Define Function:

The function findSum() takes an integer and returns the sum of its digits.

Use while Loop Inside the Function:

The logic inside the function is similar to the while loop approach.

Call the function from main() method.

SumOfDigitsExample3.java

Output:

The sum of digits: 26

In the following program, we have reduced the lines of code and implement the steps in a for loop only.

SumOfDigitsExample4.java

Output:

The sum of digits: 35

Using Recursion

Recursion provides an elegant way to solve this problem by repeatedly breaking it down into smaller sub-problems.

Define Recursive Function:

The function calls itself with a reduced number until the base case (number == 0) is reached.

Base Case and Recursive Step:

Base case: When number is 0, return 0.

Recursive step: Add the last digit to the result of the recursive call with the remaining number.

Here is the code for this approach:

SumOfDigitsExample5.java

Output:

The sum of digits: 28

Using Recursion with an Instance Variable

This variant of the recursion method uses an instance variable to store the sum.

Define Class with Instance Variable:

Define a class with an instance variable sum.

Recursive Function:

The function updates the instance variable sum and calls itself until the number becomes 0.

SumOfDigitsExample6.java

Output:

Enter a number: 983451
The sum of digits: 30

Conclusion

Each approach has its own advantages:

  • While Loop: Simple and straightforward, ideal for beginners.
  • For Loop: More concise and can be preferred for simpler logic.
  • Function: Enhances code modularity and reusability.
  • Recursion: Elegant and showcases a different programming paradigm, suitable for problems that can be broken down into similar sub-problems.
  • Recursion with Instance Variable: Useful when encapsulating the logic within an instance context.