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

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

  1. Accept the number n. If n is not positive, print an invalid-input message and stop.
  2. Copy n into t, so that n is not changed.
  3. While t > 9 (it has more than one digit):
    • copy t into x and set s = 0;
    • while x > 0, add the last digit x % 10 to s and remove it with x = x / 10;
    • replace t by s, the sum of its digits.
  4. When the loop stops, t is a single digit. If t == 1, print that n is 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

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
nintThe number entered by the user
tintWorking copy of n that is replaced again and again by its digit sum
xintCopy of t whose digits are added, reduced by 1 digit each pass
sintAccumulates the sum of the digits of x

Sample input and output

Sample 1

Enter a number: 1234
1234 is a Magic Number

Sample 2

Enter a number: 29
29 is not a Magic Number

Sample 3

Enter a number: 0
Invalid input. Enter a positive number.

One value per line

Dry run

Input: n = 1234. t is set to 1234; each row is one pass of the while (t > 9) loop.

Passt beforet > 9?Digits addedt after
11234Yes1 + 2 + 3 + 410
210Yes1 + 01
31No, loop stops-1

t == 1 is true, so the program prints 1234 is a Magic Number, the same as the real run above.

Variations

  1. Range "Accept two integers m and n and display all the magic numbers between m and n."
  2. Recursion "Write the magic number check using a recursive method that calls itself with the sum of the digits."
  3. Single digit "Display the single digit obtained after repeatedly adding the digits of a number."