You are here : Home / Core Java Tutorials / Interview Programs (beginner to advanced) in java / Level2 programs in java (intermediate)
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.
Must read: write a program to find out all possible unique subString present in a given string in java.
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.
RELATED LINKS>