Prerequisite: Recursion in Java
In the previous article, we have discussed about Java Program to Find Sum of All Numbers in an Array by Using Recursion
In this program we are going to see how to find sum of digits of a number using by Recursion in Java programming language.
Java Program to Find Sum of Digits of a Number by Using Recursion
Lets assume there is a number say N = 1234
Then the sum of the digits of N = 1+2+3+4 = 10
Now let’s see different ways to find sum of digits of a number by using Recursion.
Method-1: Java Program to Find Sum of Digits of a Number By Using Static Input and Recursion
Approach:
- Declare and initialize an integer variable say ‘
n
’ - Call a user defined method
calculateSum()
and pass ‘n
’ as parameter. - Inside the user defined method we will check the number. If the number is
<=0
then the method returns 0 else it calls the same method ascalculateSum(n/10) + n%10
and return the value to the main method. - Now the value of the user defined method
calculateSum()
is stored in an integer variable say ‘sum
’. - Print the value of
sum
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // declare and initialize an integer variable ‘n’ int n = 7869; //call the user defined method calculateSum() //and store the result value inside an integer variable say ‘sum’ int sum = calculateSum(n); System.out.println("The sum of digits of the number is: "+sum); } //calculateSum() method static int calculateSum(int n) { //checking if if number is less than 0 then returning 0 as result if (n <= 0) return 0; //else adding the digits of number by using recursion technique else return n%10 + calculateSum(n/10); } }
Output: The sum of digits of the number is: 30
Method-2: Java Program to Find Sum of Digits of a Number By Using Dynamic Input and Recursion
Approach:
- Declare and initialize an integer variable say ‘
n
’ - Call a user defined method
calculateSum()
and pass ‘n
’ as parameter. - Inside the user defined method we will check the number. If the number is
<=0
then the method returns 0 else it calls the same method ascalculateSum(n/10) + n%10
and return the value to the main method. - Now the value of the user defined method
calculateSum()
is stored in an integer variable say ‘sum
’. - Print the value of
sum
Program:
import java.util.*; import java.io.*; public class Main { public static void main(String[] args) { // create a scanner class object Scanner s = new Scanner(System.in); //Taking input of a number System.out.println("Enter the number:"); int n = s.nextInt(); //call the user defined method calculateSum() //and store the result value inside an integer variable say ‘sum’ int sum = calculateSum(n); System.out.println("The sum of digits of the number is: "+sum); } //calculateSum() method static int calculateSum(int n) { //checking if if number is less than 0 then returning 0 as result if (n <= 0) return 0; //else adding the digits of number by using recursion technique else return n%10 + calculateSum(n/10); } }
Output: Enter the number: 2543 The sum of digits of the number is: 14
Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.
Related Java Programs: