Solved ProgramsISCClass 12Computer Science

Sum of the digits: An intriguing Recursion program

Sum of the digits: solved Java program for ISC Class 12 Computer Science with complete BlueJ code, explanation and sample output.

Updated
Solved Programs

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.
Recursion

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
Armstrong Number with Recursion
Binary Search

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

sum of the digits