Recently this question to asked was one of my reader, which inspired me to write this tutorial. There was usual check to solve this problem using both recursion and iteration. To be frank,

*calculating sum of digit of an integral number*, is not difficult, but I have still seen quite a few programmers fumbles, even after providing hint in terms of division and modules operator. Key point here is to know how to use division and modules operator in Java. For this kind of exercise including reversing a number, where you need to find digits from a number,**use division operator to remove right**, and**use modules operator or % to get right most digits**. For example if you have number 1234 than 1234/10 will give you 123 i.e. right most digit 4 is removed, while 1234%10 will give you 4, which is the right most digit in that number. If you know this property, you can easily solve lots or problems which are related to reversing numbers e.g. checking if a number is palindrome or finding Armstrong numbers in Java.##
__Java program to calculate sum of digits in a number__

Here is complete Java code example to find sum of digits using recursion in Java. This Java example, also includes an iterative solution of this problem to prepare follow-up questions from Interviewer.

By the way, you can also use this example to learn Recursion in Java. It’s a tricky concept, and examples like this, certainly helps to understand and apply recursion better.

By the way, you can also use this example to learn Recursion in Java. It’s a tricky concept, and examples like this, certainly helps to understand and apply recursion better.

/** * Java program to calculate sum of digits for a number using recursion and iteration. * Iterative solution uses while loop here. * * @author Javin Paul */ public class SumOfDigit { public static void main(String args[]) { System.out.println( "Sum of digit using recursion for number 123 is " + sumOfDigits(123)); System.out.println( "Sum of digit using recursion for number 1234 is " + sumOfDigits(1234)); System.out.println( "Sum of digit from recursive function for number 321 is " + sumOfDigits(321)); System.out.println( "Sum of digit from recursive method for number 1 is " + sumOfDigits(1)); System.out.println( "Sum of digit using Iteration for number 123 is " + sumOfDigitsIterative(123)); System.out.println( "Sum of digit using while loop for number 1234 is " + sumOfDigitsIterative(1234)); } public static int sumOfDigits(int number){ if(number/10 == 0) return number; return number%10 + sumOfDigits(number/10); } public static int sumOfDigitsIterative(int number){ int result = 0; while(number != 0){ result = result + number%10; number = number/10; } return result; } } Output: Sum of digit using recursion for number 123 is 6 Sum of digit using recursion for number 1234 is 10 Sum of digit from recursive function for number 321 is 6 Sum of digit from recursive method for number 1 is 1 Sum of digit using Iteration for number 123 is 6 Sum of digit using while loop for number 1234 is 10

That's all on

**How to find sum of digits of a number using recursion in Java**. You should be able to write this method using both Iteration i.e. using loops, and using Recursion i.e. without using loops in Java.**Further Reading**

Algorithms and Data Structures - Part 1 and 2

Java Fundamentals, Part 1 and 2

Cracking the Coding Interview - 189 Questions and Solutions

## 2 comments :

I may do it in this way, it's easier for me to understand,

public static int sumOfDigits(int digit)

{

int re = 0;

while(digit != 0)

{

re += digit%10;

digit /= 10;

}

return re;

}

Sum of digits os the addition of the digits present in the program.you can get the code from http://letusprogram.wordpress.com/2013/07/12/sum-of-digits-in-java/

## Post a Comment