Sum of digits in number using recursion in java


In this core java programming tutorial we will write a program to find out substring in given string in java.



Hi! in this post we are going to write a program that will help us in calculating sum of digits in number using recursion.



Full Program/SourceCode/ Example to find out substring in given string in java >
/** Copyright (c), AnkitMittal  JavaMadeSoEasy.com */
public class SumOfDigitsInNumberRecursion {
   public static void main(String...args){
         
          int number=1234;
         
          System.out.println("number     : "+number);
          System.out.println("sum of digits : "+sumOfDigitsUsingRecursion(number));
         
   }
  
   /**
   * method returns sum of digits using recursion.
   */
   static int sum=0;
   public static int sumOfDigitsUsingRecursion(int number){
         
          if(number == 0){
        return sum;
       }
          else{
        sum = sum + (number%10);
        sumOfDigitsUsingRecursion(number/10);
       }
       return sum;
   }
}
/*OUTPUT
number     : 1234
sum of digits : 10
*/




So in this core java programming tutorial we learned how to write a program to find out substring in given string in java.

Previous program                                                                  Next program




RELATED LINKS>



>Pattern/Pyramid generating programs



eEdit
Must read for you :