Check Magic Number in Java

What you’ll learn

  • Repeated digit sum approach for magic number checks.
  • Single check and range listing in Java.
  • How digital root connects to this interview pattern.

Prerequisites

  • Basic Java syntax, loops, conditionals, and methods.
  • Comfort with integer arithmetic and console input/output.

Math definition

This problem is driven by a deterministic numeric rule: for each valid input, apply the same formula repeatedly and classify the result.

Intuition with examples

Work a small input by hand first, then map each step to one Java loop iteration. This makes the control flow and stopping condition clear.

Live preview

Live result
Press "Run check" to see steps.

Algorithm

Goal: check whether repeated digit summing ends at 1.

Reduce to one digit

While n > 9, set n to the sum of its digits using % 10 and / 10.

Final verdict

If the final single digit is 1, classify it as magic; otherwise not magic.

📜 Pseudocode

Pseudocode
function isMagic(n):
    while n > 9:
        n = digitSum(n)
    return n == 1
1

Check single number (19)

java
public class Main {
    static boolean isMagicNumber(int num) {
        while (num > 9) {
            int sum = 0;
            while (num > 0) {
                sum += num % 10;
                num /= 10;
            }
            num = sum;
        }
        return num == 1;
    }

    public static void main(String[] args) {
        int number = 19;
        if (isMagicNumber(number)) {
            System.out.println(number + " is a Magic Number.");
        } else {
            System.out.println(number + " is not a Magic Number.");
        }
    }
}
📤 Output
19 is a Magic Number.

Explanation

The outer loop reduces the number to one digit by repeated digit sums; if that digit is 1, the method returns true.

2

Magic numbers in range 1-50

java
public class Main {
    static boolean isMagicNumber(int num) {
        while (num > 9) {
            int sum = 0;
            while (num > 0) {
                sum += num % 10;
                num /= 10;
            }
            num = sum;
        }
        return num == 1;
    }

    public static void main(String[] args) {
        System.out.println("Magic Numbers in the range 1 to 50:");
        for (int i = 1; i <= 50; i++) {
            if (isMagicNumber(i)) {
                System.out.print(i + " ");
            }
        }
        System.out.println();
    }
}
📤 Output
Magic Numbers in the range 1 to 50:
1 10 19 28 37 46

Explanation

The same helper is reused for each number in the range and prints only values whose final reduced digit equals 1.

Optimization notes

Keep it simple. Prefer clear loops and method extraction before micro-optimizations.

Reuse computed values. Avoid recomputing the same sub-result inside nested loops.

❓ FAQ

A positive integer is magic if repeated digit sums end at 1.
Yes. It is already single-digit and equal to 1.
No. Happy numbers use sum of squared digits.
This page uses positive integers only.
Magic means digital root equals 1.
Repeated summing is tiny for int-size inputs; conceptually around O((log n)^2).

🔄 Input / output examples

Use the sample programs as reference: provide valid numeric input and verify the output pattern matches the problem definition.

Input nResult
19Magic
18Not magic
1Magic
28Magic

Edge cases

Bounds

Smallest valid input

Confirm behavior for minimum accepted values.

Validation

Invalid input

Guard against non-numeric or out-of-range values when input is user-provided.

⏱️ Time and space complexity

ApproachTimeExtra space
Repeated digit-sum loopsO((log n)^2) conceptualO(1)
Digital-root formulaO(1)O(1)
Range scanU single checks for [1, U]O(1)

Summary

  • Use a direct Java implementation of the numeric rule.
  • Validate input and test boundary cases.
  • Discuss complexity clearly in interviews.
Did you know?

Repeatedly summing decimal digits until one digit remains is digital root logic. In this tutorial, a number is magic if that final digit is 1.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful