Find Sum of Digits of a Number Using Recursion in Java



In this article, we will understand how to find the sum of digits of a number using recursion in Java. 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.

Problem Statement

Write a program in Java to find the sum of 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

Different Approaches

Below are the different approaches to find sum of digits of a number using recursion ?

Steps to find the sum of digits of a number using user-defined input

Following are the steps to find the sum of 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.
  • Capture the user input using the nextInt() method of the Scanner object.
  • Call the digitSum function, passing the user input as an argument.
  • Print the result, 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;
      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

Steps to find the sum of digits of a number using predefined input

Following are the steps to find the sum of 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.
  • Call 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
Updated on: 2024-09-16T23:24:17+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements