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
main()reads the number intonum. If it is 0 or negative it prints an invalid-input message.- Otherwise
main()creates the objectobwithnew Magic(num). The constructorMagic(int nn)stores the value in the data membern. ob.display()callsisMagic()and prints the result.isMagic()copiesnintotand repeats whilet > 9:tis replaced bydigitSum(t).
digitSum(x)returns the sum of the digits ofx, usingx % 10to take a digit andx / 10to remove it.isMagic()returnstrueif the single digit left intis 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
| Variable | Data Type | Purpose |
|---|---|---|
| n | int | Data member that stores the number to be checked |
| nn | int | Parameter of the constructor that carries the number into n |
| x | int | Parameter of digitSum(); the number whose digits are added |
| s | int | Local variable of digitSum() that accumulates the sum of the digits |
| t | int | Working copy of n in isMagic() that is replaced by its digit sum until one digit remains |
| args | String[] | Command line arguments of main() (not used) |
| sc | Scanner | Reads the number typed at the keyboard in main() |
| num | int | Number entered by the user |
| ob | Magic | Object of class Magic created in main() |
Sample input and output
Sample 1
Enter a number: 19
19 is a Magic NumberSample 2
Enter a number: 9999
9999 is not a Magic NumberSample 3
Enter a number: 0
Invalid input. Enter a positive number.Dry run
Input: num = 9999, so the constructor sets n = 9999 and isMagic() sets t = 9999.
| Pass | t before | t > 9? | Digits added | t after |
|---|---|---|---|---|
| 1 | 9999 | Yes | 9 + 9 + 9 + 9 | 36 |
| 2 | 36 | Yes | 3 + 6 | 9 |
| 3 | 9 | No, 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
- 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."
- Single digit "Display the single digit obtained by repeatedly adding the digits."