ISC Practicals and Theory - Smith Number
Updated 21 Oct 2026
A Smith number is a composite number whose sum of digits is equal to the sum of the digits of all its prime factors, each factor counted as many times as it occurs. This program is written in the main method alone: it finds the prime factors, adds the digits of each, and compares the total with the digit sum of the number.
Definition
A Smith number is a composite number $N$ for which
sum of the digits of $N$ = sum of the digits of all the prime factors of $N$ (a repeated prime factor is counted each time it occurs).
- $22 = 2 \times 11$. Digit sum of $22$ is $2 + 2 = 4$. Digit sums of the factors: $2$ gives $2$ and $11$ gives $1 + 1 = 2$, total $4$. Both are $4$, so $22$ is a Smith number.
- $4937775 = 3 \times 5 \times 5 \times 65837$. Digit sum of the number is $4+9+3+7+7+7+5 = 42$. Digit sums of the factors: $3 + 5 + 5 + (6+5+8+3+7) = 3 + 5 + 5 + 29 = 42$. So it is a Smith number.
- $15 = 3 \times 5$. Digit sum of $15$ is $6$, but the factors give $3 + 5 = 8$. Not a Smith number.
- A prime number is never a Smith number, because a Smith number must be composite (a prime has only itself as its prime factor, so the two sums would always be equal and the test would mean nothing). $1$ is also not a Smith number as it is not composite.
Algorithm
- Accept the number
num. Ifnum < 1, displayInvalid inputand stop. - Copy
numintonand settotal = 0andcount = 0. - For
ffrom 2 up ton, whilen % f == 0:fis a prime factor: find the sum of its digits ins(using% 10and/ 10) and add it tototal;- add 1 to
countand dividenbyf.
- Find the digit sum of
numitself inds. - If
num == 1, it is not a Smith number. Ifcount == 1, the number has only one prime factor (itself), so it is prime and not a Smith number. Otherwise, iftotal == ds, it is a Smith number; if not, it is not.
Program
Everything is written inside main: the for loop with the while loop inside it finds the prime factors, and small while loops find the digit sums.
import java.util.Scanner;
public class Smith
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt(); // the number to be checked
if (num < 1)
{
System.out.println("Invalid input");
}
else
{
int n = num; // copy of num that is divided by its factors
int total = 0; // sum of the digits of all the prime factors
int count = 0; // number of prime factors, repeated ones counted
for (int f = 2; f <= n; f++)
{
while (n % f == 0)
{
int x = f, s = 0; // s is the digit sum of the factor f
while (x > 0)
{
s = s + x % 10;
x = x / 10;
}
total = total + s;
count++;
n = n / f;
}
}
int y = num, ds = 0; // ds is the digit sum of num
while (y > 0)
{
ds = ds + y % 10;
y = y / 10;
}
if (num == 1)
System.out.println("1 is not a Smith number");
else if (count == 1)
System.out.println(num + " is a prime number, so it is not a Smith number");
else if (total == ds)
System.out.println(num + " is a Smith number");
else
System.out.println(num + " is not a Smith number");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
num | int | The number entered by the user |
n | int | Copy of num that is divided by its prime factors |
total | int | Sum of the digits of all the prime factors |
count | int | Number of prime factors, repeated ones counted each time |
f | int | Loop counter; the factor being tried |
x | int | Copy of the factor f whose digits are added |
s | int | Digit sum of the factor f |
y | int | Copy of num whose digits are added |
ds | int | Digit sum of num |
Sample input and output
Sample 1
Enter a number: 22
22 is a Smith numberSample 2
Enter a number: 15
15 is not a Smith numberSample 3
Enter a number: 13
13 is a prime number, so it is not a Smith numberSample 4
Enter a number: -5
Invalid inputDry run
Input: num = 22. The factor loop starts with n = 22, total = 0, count = 0.
| f | n % f | Action | n | total | count |
|---|---|---|---|---|---|
| 2 | 0 | factor found: add the digit sum of 2, which is 2, then n = 22 / 2 | 11 | 2 | 1 |
| 2 | 1 | not a factor now, so the inner loop ends | 11 | 2 | 1 |
| 3 to 10 | not 0 | none is a factor | 11 | 2 | 1 |
| 11 | 0 | factor found: add the digit sum of 11, which is 2, then n = 11 / 11 | 1 | 4 | 2 |
| 11 | 1 | not a factor now, so the inner loop ends | 1 | 4 | 2 |
Now f becomes $12$, and f <= n is false, so the loop ends and total = 4. The digit sum of 22 is $2 + 2 = 4$, so ds = 4. Since count = 2 (not $1$) and the two sums are equal, the program shows that $22$ is a Smith number, as in the first sample run.
Variations
- Range: "Display all Smith numbers between m and n."
- Recursion: "Find the sum of the digits of a number using recursive technique and use it to check a Smith number."
- Prime factors: "Display the prime factors of a number and the sum of their digits."
- Niven number: "Check whether a number is divisible by the sum of its digits."