PUBLISHED ON: APRIL 16, 2021
Java Program to Find Sum of Digits of a Number using Recursion
In this tutorial, we will learn how to find the sum of all digits of a number using recursion. A recursive function is a function that calls itself repeatedly. Here, we will first ask the user to initialize the number and then will find all the digits and calculate their sum by calling the function recursively. But before moving forward if you are not familiar with the concept of loops in java, then do check the article on Loops in Java.
Input: Enter the number: 564
Output: The sum of all the digits is: 15
Let us look at the examples to understand how to find the sum of the digits using recursion.
Program 1: Java Program Sum Of digits Of A Number
In the below example, we will see how to find the sum of digits of a number using recursion.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number.
- Ask the user to initialize the variable.
- Declare a user-defined function to calculate the sum of digits of the number using recursion.
- Call the function recursively to calculate the sum of digits.
- Display the sum of digits.
- Stop.
The below example illustrates the implementation of the above algorithm.
//Java Program to find the sum of digits using recursion
import java.util.*;
public class Main
{
public static int sum_of_digit(int num)
{
if (num == 0)
return 0;
return (num % 10 + sum_of_digit(num / 10));
}
// Driver Program
public static void main(String args[])
{
//Take input from the user
//Create an instance of the Scanner Class
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number: ");
int num=sc.nextInt();
int res=sum_of_digit(num);
System.out.println("The sum of digits is: "+res);
}
}
Enter a number 854
The sum of digits of the number is 17
Program 2: Java Program Sum Of digits Of A Number
In the below example, we will see how to find the sum of digits of a number using recursion.
Algorithm:
- Start
- Create an instance of the Scanner class.
- Declare a variable to store the number.
- Ask the user to initialize the variable.
- Declare a user-defined function to calculate the sum of digits of the number using recursion.
- Call the function recursively to calculate the sum of digits.
- Return the sum calculated.
- Print the result.
- Stop.
The below example illustrates the implementation of the above algorithm.
//Java Program to find the sum of digits using recursion
import java.util.*;
public class Main
{
int sum=0;
int sumOfDigits(long num)
{
if(num!=0)
{
sum+=num%10;
num/=10;
sumOfDigits(num);
}
return sum;
}
//Driver code
public static void main(String arg[])
{
long num,res;
Main main=new Main();
//Take input from the user
//Create an instance of the Scanner class
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number ");
num=sc.nextLong();
System.out.println("The sum of digits of the number is "+main.sumOfDigits(num));
}
}
Enter a number 4567854
The sum of digits of the number is 39