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

ISC Practicals and Theory - Magic Number (OOP)

Updated 20 Oct 2026

A magic number is a number whose digits are added repeatedly until one digit remains, and that digit is 1. Composite Magic Number is a different concept: a composite number whose repeated digit sum is 1. This version is written as a class Magic with a data member, a constructor and methods that each do one job, and main() creates an object and calls them.

Definition

Add the digits of the number; if the sum has more than one digit, add its digits again, until one digit is left. If that digit is 1 the number is magic. The number 1 is magic, while 0 and negative numbers are treated as invalid input.

  • $19$: $1 + 9 = 10$, then $1 + 0 = 1$, so $19$ is magic.
  • $9999$: $9 + 9 + 9 + 9 = 36$, then $3 + 6 = 9$, so $9999$ is not magic.

Algorithm

  1. main() reads the number into num. If it is 0 or negative it prints an invalid-input message.
  2. Otherwise main() creates the object ob with new Magic(num). The constructor Magic(int nn) stores the value in the data member n.
  3. ob.display() calls isMagic() and prints the result.
  4. isMagic() copies n into t and repeats while t > 9:
    • t is replaced by digitSum(t).
  5. digitSum(x) returns the sum of the digits of x, using x % 10 to take a digit and x / 10 to remove it.
  6. isMagic() returns true if the single digit left in t is 1.

Program

The constructor stores the number, digitSum() adds the digits, isMagic() repeats the digit sum until one digit is left, and display() prints the message.

import java.util.*;

/* Class 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
{
    int n;      // stores the number to be checked

    /* Constructor: assigns the number to the data member n.
       Parameter nn: the number to be checked. */
    Magic(int nn)
    {
        n = nn;
    }

    /* Returns the sum of the digits of the number x.
       Parameter x: the number whose digits are added. */
    int digitSum(int x)
    {
        int s = 0;              // accumulates the sum of digits
        while (x > 0)
        {
            s = s + x % 10;     // adds the last digit
            x = x / 10;         // removes the last digit
        }
        return s;
    }

    /* Returns true if n is a magic number, otherwise false.
       The digit sum is repeated while the value has more than one digit. */
    boolean isMagic()
    {
        int t = n;              // working copy so that n is not changed
        while (t > 9)           // more than one digit
        {
            t = digitSum(t);
        }
        return t == 1;          // magic only if the single digit is 1
    }

    /* Displays whether the number n is magic or not. */
    void display()
    {
        if (isMagic())
            System.out.println(n + " is a Magic Number");
        else
            System.out.println(n + " is not a Magic Number");
    }

    /* main: reads a number, creates the object and calls display().
       Parameter args: command line arguments (not used). */
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);    // reads input from the keyboard
        System.out.print("Enter a number: ");
        int num = sc.nextInt();                 // number entered by the user
        if (num <= 0)                           // a magic number must be positive
            System.out.println("Invalid input. Enter a positive number.");
        else
        {
            Magic ob = new Magic(num);          // object of class Magic
            ob.display();
        }
    }
}

Variable description table

VariableData TypePurpose
nintData member that stores the number to be checked
nnintParameter of the constructor that carries the number into n
xintParameter of digitSum(); the number whose digits are added
sintLocal variable of digitSum() that accumulates the sum of the digits
tintWorking copy of n in isMagic() that is replaced by its digit sum until one digit remains
argsString[]Command line arguments of main() (not used)
scScannerReads the number typed at the keyboard in main()
numintNumber entered by the user
obMagicObject of class Magic created in main()

Sample input and output

Sample 1

Enter a number: 19
19 is a Magic Number

Sample 2

Enter a number: 9999
9999 is not a Magic Number

Sample 3

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

One value per line

Dry run

Input: num = 9999, so the constructor sets n = 9999 and isMagic() sets t = 9999.

Passt beforet > 9?Digits addedt after
19999Yes9 + 9 + 9 + 936
236Yes3 + 69
39No, loop stops-9

t == 1 is false, so isMagic() returns false and the program prints 9999 is not 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."
  3. Single digit "Display the single digit obtained by repeatedly adding the digits."