ISC Practicals and Theory - Composite Magic Number
Updated 21 Oct 2026
A Composite Magic Number is a number that is both composite and magic. This program is written in the main method alone: it counts the factors of the number to test whether it is composite, then finds the repeated digit sum to test whether it is magic.
Definition
A natural number $N$ is a composite number if it has more than two factors. So $1$ is not composite (one factor) and a prime number is not composite (exactly two factors).
A number is a magic number if repeatedly adding its digits, until a single digit remains, gives $1$.
A number that is composite and magic is a Composite Magic Number.
- $28$: $2 + 8 = 10$, then $1 + 0 = 1$, so it is magic. Its factors are $1, 2, 4, 7, 14, 28$, so it is composite. Hence $28$ is a Composite Magic Number.
- $19$: $1 + 9 = 10$, then $1 + 0 = 1$, so it is magic, but $19$ is prime. It is not a Composite Magic Number.
- $12$: it is composite, but $1 + 2 = 3$, so it is not magic.
Algorithm
- Accept the number
num. Ifnum < 1, displayInvalid inputand stop. - Count the factors: set
count = 0and, forifrom 1 tonum, add 1 tocountwhenevernum % i == 0. - The number is composite if
count > 2; store this in the booleancomposite. - Find the repeated digit sum: set
s = num. Whiles > 9, sett = 0, add every digit ofstot(usings % 10ands / 10), then sets = t. - If
compositeis true ands == 1, display thatnumis a Composite Magic Number. Otherwise display that it is not.
Program
The for loop in main counts the factors, and the two nested while loops find the repeated digit sum.
import java.util.Scanner;
public class CompositeMagic
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
if (num < 1)
{
System.out.println("Invalid input");
}
else
{
int count = 0; // number of factors of num
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
count++;
}
boolean composite = count > 2; // a composite number has more than two factors
int s = num; // repeated digit sum
while (s > 9)
{
int t = 0; // sum of the digits of s
while (s > 0)
{
t = t + s % 10;
s = s / 10;
}
s = t;
}
if (composite && s == 1)
System.out.println(num + " is a Composite Magic Number");
else
System.out.println(num + " is not a Composite Magic Number");
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the number typed by the user |
num | int | The number entered by the user |
count | int | Number of factors of num |
i | int | Loop counter that tries each possible factor |
composite | boolean | True if num has more than two factors |
s | int | Starts as num and is reduced to its repeated digit sum |
t | int | Sum of the digits of s in the current round |
Sample input and output
Sample 1
Enter a number: 28
28 is a Composite Magic NumberSample 2
Enter a number: 19
19 is not a Composite Magic NumberSample 3
Enter a number: 12
12 is not a Composite Magic NumberSample 4
Enter a number: -5
Invalid inputDry run
Input: num = 28
First, the for loop counts the factors of $28$:
| i | num % i | Factor? | count |
|---|---|---|---|
| 1 | 0 | Yes | 1 |
| 2 | 0 | Yes | 2 |
| 3 | 1 | No | 2 |
| 4 | 0 | Yes | 3 |
| 5 | 3 | No | 3 |
| 6 | 4 | No | 3 |
| 7 | 0 | Yes | 4 |
| 8 to 13 | not zero | No | 4 |
| 14 | 0 | Yes | 5 |
| 15 to 27 | not zero | No | 5 |
| 28 | 0 | Yes | 6 |
count = 6, which is more than $2$, so composite is true.
Next, the digit sum starts with s = 28:
| Round | s at start | Digit taken (s % 10) | t after the inner loop | s after the round |
|---|---|---|---|---|
| 1 | 28 | 8, then 2 | 10 | 10 |
| 2 | 10 | 0, then 1 | 1 | 1 |
Now s = 1, which is not greater than $9$, so the loop stops. Both conditions hold (composite is true and s == 1), so the program shows that $28$ is a Composite Magic Number, as in the first sample run.
Variations
- Range: "Display all Composite Magic Numbers between m and n."
- Magic number: "Check whether a number is a magic number: the repeated sum of its digits is 1."
- Recursive digit sum: "Find the repeated digit sum of a number using recursive technique."