- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Java Program to Find Sum of Digits of a Number using Recursion

In this article, we will understand how to find sum of digits of a number using recursion. A recursive function is a function that calls itself multiple times until a particular condition is satisfied.

Recursion is the process of repeating items in a self-similar way. In programming languages, if a program allows you to call a function inside the same function, then it is called a recursive call of the function.

Many programming languages implement recursion by means of stacks. Generally, whenever a function (caller) calls another function (callee) or itself as callee, the caller function transfers execution control to the callee. This transfer process may also involve some data to be passed from the caller to the callee.

Below is a demonstration of the same −

**Input**

Suppose our input is −

Enter the number : 12131415

**Output**

The desired output would be −

The Sum of digits of 12131415 is 18

## Algorithm

Step 1 - START Step 2 - Declare two integer values namely my_input and my_result Step 3 - Read the required values from the user/ define the values Step 4 - A recursive function ‘digitSum’ is defined which takes an integer as input. The function computes the reminder by re-iterating over the function multiple times, until the base condition is reached. Step 5 - The recursive function ‘digitSum’ is called and its result is assigned to ‘my_result’ Step 6 - Display the result Step 7 - Stop

## Example 1

Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .

import java.util.Scanner; public class Sum{ public static void main(String args[]){ int my_input, my_result; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the number : "); my_input = my_scanner.nextInt(); my_result = digitSum(my_input); System.out.println("The Sum of digits of " + my_input + " is " + my_result); } static int digitSum(int n){ if (n == 0) return 0; return (n % 10 + digitSum(n / 10)); } }

## Output

Required packages have been imported A reader object has been defined Enter the number : 12131415 The Sum of digits of 12131415 is 18

## Example 2

Here, the integer has been previously defined, and its value is accessed and displayed on the console.

public class Sum{ public static void main(String args[]){ int my_input = 12131415; System.out.println("The number is defined as : " +my_input); int my_result = digitSum(my_input); System.out.println("The Sum of digits of " + my_input + " is " + my_result); } static int digitSum(int n){ if (n == 0) return 0; return (n % 10 + digitSum(n / 10)); } }

## Output

The number is defined as : 12131415 The Sum of digits of 12131415 is 18

- Related Articles
- C# program to find the sum of digits of a number using Recursion
- Python Program to Find the Sum of Digits in a Number without Recursion
- How to find the sum of digits of a number using recursion in C#?
- Java Program to Find Factorial of a Number Using Recursion
- Java Program to Find Sum of N Numbers Using Recursion
- Java Program to Find the Sum of Natural Numbers using Recursion
- Java program to find the factorial of a given number using recursion
- C++ Program to Find Factorial of a Number using Recursion
- C++ program to Find Sum of Natural Numbers using Recursion
- C program to find sum of digits of a five digit number
- 8086 program to find sum of digits of 8 bit number
- 8085 program to find sum of digits of 8 bit number
- C++ program to find sum of digits of a number until sum becomes single digit
- Golang Program to Find the Sum of Natural Numbers using Recursion
- Golang Program to Find the Sum of N Numbers using Recursion