ISC Practicals and Theory - Magic Number
Updated 21 Oct 2026
A magic number is a number whose digits are added again and again until a single digit is left, and that digit is 1. Composite Magic Number is a different concept: a composite number whose repeated digit sum is 1. This note gives the method, an ISC-style Java program written in the main method alone, sample runs and a dry run.
Definition
Find the sum of the digits of the number. If the sum has more than one digit, find the sum of its digits again. Keep repeating until one digit remains. If that digit is 1 the number is a Magic Number. The number 1 is magic, since it is already a single digit equal to 1, while 0 and negative numbers are treated as invalid input.
- $1234$: $1 + 2 + 3 + 4 = 10$, then $1 + 0 = 1$. So $1234$ is magic.
- $29$: $2 + 9 = 11$, then $1 + 1 = 2$. So $29$ is not magic.
Algorithm
- Accept the number
n. Ifnis not positive, print an invalid-input message and stop. - Copy
nintot, so thatnis not changed. - While
t > 9(it has more than one digit):- copy
tintoxand sets = 0; - while
x > 0, add the last digitx % 10tosand remove it withx = x / 10; - replace
tbys, the sum of its digits.
- copy
- When the loop stops,
tis a single digit. Ift == 1, print thatnis a Magic Number. Otherwise print that it is not.
Program
The outer while loop in main repeats the digit sum while the value has more than one digit; the inner while loop adds the digits once.
import java.util.*;
/* Program Magic checks whether a number is a magic number: the sum of its digits
is found again and again until a single digit remains; if it is 1 the number is magic. */
public class Magic
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in); // reads input from the keyboard
System.out.print("Enter a number: ");
int n = sc.nextInt(); // the number entered by the user
if (n <= 0)
{
System.out.println("Invalid input. Enter a positive number.");
}
else
{
int t = n; // working copy so that n is not changed
while (t > 9) // more than one digit
{
int x = t, s = 0; // s accumulates the sum of digits of x
while (x > 0)
{
s = s + x % 10; // adds the last digit
x = x / 10; // removes the last digit
}
t = s;
}
if (t == 1) // magic only if the single digit is 1
System.out.println(n + " is a Magic Number");
else
System.out.println(n + " is not a Magic 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 |
n | int | The number entered by the user |
t | int | Working copy of n that is replaced again and again by its digit sum |
x | int | Copy of t whose digits are added, reduced by 1 digit each pass |
s | int | Accumulates the sum of the digits of x |
Sample input and output
Sample 1
Enter a number: 1234
1234 is a Magic NumberSample 2
Enter a number: 29
29 is not a Magic NumberSample 3
Enter a number: 0
Invalid input. Enter a positive number.Dry run
Input: n = 1234. t is set to 1234; each row is one pass of the while (t > 9) loop.
| Pass | t before | t > 9? | Digits added | t after |
|---|---|---|---|---|
| 1 | 1234 | Yes | 1 + 2 + 3 + 4 | 10 |
| 2 | 10 | Yes | 1 + 0 | 1 |
| 3 | 1 | No, loop stops | - | 1 |
t == 1 is true, so the program prints 1234 is a Magic Number, the same as the real run above.
Variations
- Range "Accept two integers m and n and display all the magic numbers between m and n."
- Recursion "Write the magic number check using a recursive method that calls itself with the sum of the digits."
- Single digit "Display the single digit obtained after repeatedly adding the digits of a number."