
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Sum of Digits of a Number Using Recursion in Java
In this article, we will understand how to find the sum of the digits of a number using recursion in Java. A recursive function is a function that calls itself multiple times until a particular condition or base condition is met.
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.
Problem Statement
Write a program in Java to find the sum of the digits of a number using recursion. Below is a demonstration of the same.
Input Enter the number : 12131415 Output The Sum of digits of 12131415 is 18
Sum of Digits of a Number
Below are the different approaches to find the sum of the digits of a number using recursion -
User-Defined Input
Following are the steps to find the sum of the digits of a number using user-defined input -
-
First, we will import the Scanner class from the java.util package.
-
After that, we will initialize a Scanner object to read input from the user.
-
Display a message asking the user to enter a number.
-
Take the user input using the nextInt() method of the Scanner object.
-
Invoke the digitSum function, passing the user input as an argument.
-
Print the output, showing the sum of the digits of the entered number.
Example
Here, the input is being entered by the user based on a prompt.
import java.util.Scanner; public class Sum{ public static void main(String args[]){ int my_input, my_result; Scanner my_scanner = new Scanner(System.in); 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
Enter the number : 12131415 The Sum of digits of 12131415 is 18
Using Predefined Input
Following are the steps to find the sum of the digits of a number using predefined input -
-
Declare and initialize an integer variable with a predefined value (e.g., 12131415).
-
Print the predefined number to the console.
- Invoke the digitSum function, passing the predefined number as an argument.
-
Print the result, showing the sum of the digits of the predefined number.
Example
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