In this program, a number will be taken as input.
The sum of the digits of the number will be printed.

To know more about recursion check the given post.
https://java4school.com/recursion

sum of the digits
import java.util.*;
class Recur
{
    int sum=0;
    int sumofdigits(int num)
    { 
        if(num>0)
        {
            int r=num%10;
            sum+=r;
            sumofdigits(num/10);
        }
        return sum;
    } 
    public static void main()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter a number to print the sum of the digits");
        int n=sc.nextInt();
        Recur obj=new Recur();
        System.out.println("The sum of the digits is "+ obj.sumofdigits(n));   
    }
}

Input:
Enter a number
123
The sum of the digits is 6

Click the given link
https://java4school.com/armstrong-number-with-recursion
https://java4school.com/binary-search

watch basic java videos here
www.youtube.com/user/trushntej

LEAVE A REPLY

Please enter your comment!
Please enter your name here