Java program to find the sum of digits of a number using recursion function

Java program to find the sum of digits of a number using recursion function. The source code for a Java program that uses recursion to find the sum of a number’s digits. As an input, you can use any decimal number. Now we feed that number to a new function, where we utilise the modulo operator and recursion to determine the sum of digits as output.

import java.util.Scanner;
public class Main
{
int sum = 0;
public static void main(String[] args)
{
int n;
Scanner s = new Scanner(System.in);
System.out.print("Enter the numbers");
n = s.nextInt();
Main obj = new Main();
int a = obj.add(n);
System.out.println("Sum"+a);
}
int add(int n)
{
sum = n % 10;
if(n == 0)
{
return 0;
}
else
{
return sum + add(n / 10);
}

}
}

Enter the number:45
Sum:9