Finding the sum of digits of a number involves iterating over the number, getting each digit of the number and adding it to a sum.
But this approach requires a loop construct. What if you are asked to add the digits without using a loop. The answer is using recursion and this article will explain the recursive method to sum up the digits of a number.

Algorithm
Recursion means a method calling itself till some condition is met. When the condition is met, the recursive method stops calling itself.
Adding the digits of a number using recursive method should follow below algorithm.

  1. Create a method which accepts an integer argument and returns an integer value.
  2. In every call, the method should extract a digit from the number and return the sum of this digit and the value returned by calling itself with the remaining digits of the number.
  3. Keep on calling the method till the argument received is 0.

Thus, in every iteration, the value returned will be the sum of a digit and the remaining number. After the recursive method returns, it will start adding the digits of the number in reverse order(from right to left).

Example
Java program to find the sum of digits of a number without using a loop and based on the above recursive algorithm is given below.

public class DigitAdder {
   public static void main(String[] args) {
      int number = 12345;
      int sum = add(number);
      System.out.println("Sum of digits of " + number + " is " + sum);
   }
 
   /**
    * Recursive method to calculate digits of a number
    * 
    * @param number
    * @return
    */
   static int add(int number) {
      // check if the number is 0
      if (number == 0) {
	return 0;
      }
      // get the last digit of number
      int lastDigit = number % 10;
      // call this method again with the sum of digit and remaining number
      return lastDigit + add(number / 10);
   }
}

Notice that in order to get a digit from the number, use modulus(%) operator with 10. This will give you the remainder of division by 10 which will be the rightmost digit.
In order to get the remaining number with rightmost digit removed, use division operator(/) with 10. This will return the quotient of division by 10.
Thus,
12345 % 10 will return 5, and
12345 / 10 will return 1234.
When the modulus operator(%) returns the leftmost digit, then the division operator(/) will return 0 and thus the argument supplied to the recursive method will also be 0.
This means that there is no need to call the recursive method further and hence it is the terminating condition of the recursive method.

Output of the above program will be

Sum of digits of 12345 is 15

Hit the clap below if the article was useful.

3

Share your thoughts !!

Close Menu

Never Miss an article !

Get the new post delivered straight into your inbox, enter your email and hit the button

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

codippa will use the information you provide on this form to be in touch with you and to provide updates and marketing.