ISC Practicals and Theory - Sum of Digits (Recursive) (OOP)
Updated 20 Oct 2026
This program finds the sum of the digits of a number with a recursive method, using a class whose constructor stores the number. The method findSum() adds the last digit (% 10) to the sum of the remaining digits (/ 10), calling itself until no digits are left.
Definition
The sum of digits of a number is the total of its digits. For example $90507$: $9 + 0 + 5 + 0 + 7 = 21$. The last digit is k % 10 and the rest of the number is k / 10.
A recursive method needs a base case (stops without calling itself) and a recursive case (calls itself with a smaller problem).
A negative number is rejected with an error message; only 0 or a positive number is accepted.
Algorithm
- Start. In
main(), read the numberxwith aScanner. - Create the object
obwithnew DigitSum(x). The constructor copies the number into the data membernumand setstotalto 0. - Call
isValid(). It returns true only ifnumis 0 or more; otherwise print an error message and stop. - Call
compute(), which callsfindSum(num)and stores the value intotal. InsidefindSum(k):- Base case: if
k == 0, return 0. - Recursive case: otherwise return
k % 10 + findSum(k / 10). Each call removes one digit.
- Base case: if
- Call
show()to print the sum. - Stop.
For 0 the answer is 0 because the base case returns 0 at once.
Program
The constructor DigitSum() stores the number, isValid() checks it, findSum() is recursive, compute() stores its value and show() prints it.
import java.util.*;
/** Class DigitSum: stores a number and finds the sum of its digits using recursion. */
class DigitSum
{
int num; // the number whose digits are added
int total; // sum of the digits of num
/** Constructor: stores the given number and sets total to 0. */
DigitSum(int x)
{
num = x;
total = 0;
}
/** Returns true if num is 0 or more. */
boolean isValid()
{
return num >= 0;
}
/**
* Recursive method: returns the sum of the digits of k.
* k: the number for this call (k >= 0)
*/
int findSum(int k)
{
if (k == 0) // base case: no digits left
return 0;
return k % 10 + findSum(k / 10); // recursive case: last digit + sum of the rest
}
/** Calls findSum() with the stored number and keeps the value in total. */
void compute()
{
total = findSum(num);
}
/** Shows the sum. */
void show()
{
System.out.println("Sum of the digits of " + num + " = " + total);
}
/** main: reads the number, creates the object and calls the methods. */
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (0 or more): ");
int x = sc.nextInt();
DigitSum ob = new DigitSum(x); // object created with the constructor
if (ob.isValid())
{
ob.compute();
ob.show();
}
else
System.out.println("Invalid input: enter 0 or a positive number");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
num | int | Data member: the number whose digits are added |
total | int | Data member: stores the sum of the digits of num |
x | int | Constructor parameter in DigitSum(): the number received; also the local variable in main() holding the number typed by the user |
k | int | Parameter of findSum(): the number of this call, divided by 10 in each recursive call |
sc | Scanner | Object used in main() to read the number |
args | String[] | Command line arguments of main() (not used) |
ob | DigitSum | Object of the class created in main() |
Sample input and output
Sample 1
Enter a number (0 or more): 90507
Sum of the digits of 90507 = 21Sample 2
Enter a number (0 or more): 0
Sum of the digits of 0 = 0Sample 3
Enter a number (0 or more): -45
Invalid input: enter 0 or a positive numberDry run
Take $90507$. The constructor sets num = 90507. compute() calls findSum(90507).
| Call | k | k % 10 | Action | Value returned |
|---|---|---|---|---|
findSum(90507) | 90507 | 7 | 7 + findSum(9050), waits | 21 |
findSum(9050) | 9050 | 0 | 0 + findSum(905), waits | 14 |
findSum(905) | 905 | 5 | 5 + findSum(90), waits | 14 |
findSum(90) | 90 | 0 | 0 + findSum(9), waits | 9 |
findSum(9) | 9 | 9 | 9 + findSum(0), waits | 9 |
findSum(0) | 0 | - | base case | 0 |
Returning up: 0, then 9 + 0 = 9, 0 + 9 = 9, 5 + 9 = 14, 0 + 14 = 14, 7 + 14 = 21. total becomes 21 and show() prints Sum of the digits of 90507 = 21, the same as the first sample run.
Variations
- Sum of digits using a loop "Write a program to find the sum of the digits of a number using a loop."
- Reverse a number "Using recursive technique, find the reverse of a number."
- Repeated addition "Keep adding the digits of a number until a single digit is left."