Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. Accept the number num. If num < 1, display Invalid input and stop.
  2. Count the factors: set count = 0 and, for i from 1 to num, add 1 to count whenever num % i == 0.
  3. The number is composite if count > 2; store this in the boolean composite.
  4. Find the repeated digit sum: set s = num. While s > 9, set t = 0, add every digit of s to t (using s % 10 and s / 10), then set s = t.
  5. If composite is true and s == 1, display that num is 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the number typed by the user
numintThe number entered by the user
countintNumber of factors of num
iintLoop counter that tries each possible factor
compositebooleanTrue if num has more than two factors
sintStarts as num and is reduced to its repeated digit sum
tintSum of the digits of s in the current round

Sample input and output

Sample 1

Enter a number: 28
28 is a Composite Magic Number

Sample 2

Enter a number: 19
19 is not a Composite Magic Number

Sample 3

Enter a number: 12
12 is not a Composite Magic Number

Sample 4

Enter a number: -5
Invalid input

One value per line

Dry run

Input: num = 28

First, the for loop counts the factors of $28$:

inum % iFactor?count
10Yes1
20Yes2
31No2
40Yes3
53No3
64No3
70Yes4
8 to 13not zeroNo4
140Yes5
15 to 27not zeroNo5
280Yes6

count = 6, which is more than $2$, so composite is true.

Next, the digit sum starts with s = 28:

Rounds at startDigit taken (s % 10)t after the inner loops after the round
1288, then 21010
2100, then 111

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

  1. Range: "Display all Composite Magic Numbers between m and n."
  2. Magic number: "Check whether a number is a magic number: the repeated sum of its digits is 1."
  3. Recursive digit sum: "Find the repeated digit sum of a number using recursive technique."