Java Program to Find Sum of Digits in a Number using Recursion - Java Interview Question for Fresher level

 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

How to find sum of digit of a number coding question in JavaHere 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.

/**
 * 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

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.