# Java Program to Compute Sum of Digit using Recursion of a given Number

Sum of Digit of a given number is addition of all its individual digits of that number.

Eg :sum of Digit of 12345 = 5+4+3+2+1 = 15

The following code calculates Sum of all its Digits of a given number using recursion.

package com.javaonline; import javax.swing.JOptionPane; public class SumofDigit { public static void main(String[] args) { System.out.println("Enter the Number"); String input = JOptionPane.showInputDialog("Enter any column number: "); // Getting Input int sum = sod(Integer.parseInt(input)); // calling sum of digit function System.out.println("Sum of the Digit of "+input+ " is " + sum); System.exit(0); } // Computes a Sum of Digit of a number by recursion. public static int sod(int n) { if(n < 10) return n; return n%10 + sod(n/10); } }

Compiling and Running the Program will give the below output:

Enter the Number

12345

Sum of the Digit of 12345 is 15

Enter the Number

97654

Sum of the Digit of 97654 is 31