Java Cube Number Program

Beginner
6 min read
Updated: Oct 2026
2 programs
Live preview

What Is a Cube Number?

A cube number (perfect cube) is an integer n that equals k3 for some integer k. Both 0 and negative cubes count.

Remember
n is a cube number  ⇔  n = k × k × k  for some integer k

Examples:
  0  = 0³
  1  = 1³
  8  = 2³
  27 = 3³
  −8 = (−2)³
  10 → not a cube
kk³Cube?
00Yes
28Yes
327Yes
—10No

First non-negative cubes

List
0, 1, 8, 27, 64, 125, 216, 343, 512, 729, 1000

How to Check a Cube Number in Java

Four clear steps — then pick an integer scan or a cube-root check.

  1. Work with |n| if you want a nonnegative search (re-apply the sign for negatives).
  2. Find an integer k whose cube might equal the value.
  3. Verify with exact integer multiplication: k * k * k == n.
  4. Never trust a floating-point cube root alone — always verify.
MethodIdeaTimeBest for
Integer scanIncrease k while k³ <= |n|O(∛|n|)Interviews, no floating point
Math.cbrt + verifyRound Math.cbrt, then check k³ == nO(1)Quick answers, typical int sizes

Pseudocode

Pseudocode
function IsCube(n):
    target = abs(n)
    k = 0

    while k * k * k < target:
        k = k + 1

    if k * k * k != target:
        return false

    return true

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Definitionn = k³ for some integer k
Always cubes0, 1, −1
VerifyUse integer k * k * k == n
AvoidTrusting Math.cbrt without a re-check
Overflow tipUse long for the product when |n| is large
Classic check27 → yes (3³); 10 → no

Live Preview

Enter any integer to see whether it is a perfect cube and which k produces it.

Whole numbers (capped at ±999999999). Tap a chip or type a value.

Result
n = 27
k = 3
Check: 3 × 3 × 3 = 27
Verdict: CUBE NUMBER

Worked Walkthrough — n = 27

Increase k until its cube reaches or passes 27, then compare exactly.

Calculation
k = 0 → 0³ = 0  < 27
k = 1 → 1³ = 1  < 27
k = 2 → 2³ = 8  < 27
k = 3 → 3³ = 27 = 27  ✓

Result: 27 is a cube number (k = 3)

Java Programs

Start with a pure integer scan. Then use Math.cbrt with an integer verification for a short production-style check.

Example 1 — Integer Scan

Walk k upward until k³ meets or passes |n|, then compare.

Java
class CubeCheck {
    static boolean isCube(int n) {
        long target = Math.abs((long) n);
        long k = 0;

        while (k * k * k < target) {
            k++;
        }

        return k * k * k == target;
    }

    public static void main(String[] args) {
        int number = 27;

        if (isCube(number)) {
            System.out.println(number + " is a cube number.");
        } else {
            System.out.println(number + " is not a cube number.");
        }
    }
}

How It Works

  1. Store |n| in a long with Math.abs so cubing stays safer for large values.
  2. Start k at 0 and increase while k³ < target.
  3. When the loop stops, either k³ == target (cube) or k³ > target (not a cube).
  4. Negatives work because cubing preserves sign — matching on absolute value is enough.
  5. For 27, the loop lands on k = 3 and confirms 27.

Example 2 — Math.cbrt + Verify

Estimate the cube root, round to the nearest integer, then re-check with exact multiplication.

Java
class CubeCbrt {
    static boolean isCubeFast(int n) {
        long target = Math.abs((long) n);
        long k = Math.round(Math.cbrt(target));
        return k * k * k == target;
    }

    public static void main(String[] args) {
        System.out.println(isCubeFast(27)); // true
        System.out.println(isCubeFast(10)); // false
        System.out.println(isCubeFast(-8)); // true
    }
}

How It Works

  1. Math.cbrt gives an approximate real cube root of |n|.
  2. Math.round picks the nearest integer candidate k.
  3. The decisive step is integer: k * k * k == target. That removes floating-point doubt.
  4. If the rounded guess is wrong, the product will not match and the method returns false.
  5. Checks: 27 and −8 are cubes; 10 is not.

Edge Cases & Pitfalls

Check these before calling the solution done.

0 and 1

Valid cubes

0 = 0³ and 1 = 1³. Do not special-case them as false.

Negatives

Odd power keeps sign

−8 is a cube. Compare against |n|, or cube a signed k.

Float only

Always verify

Rounding alone can misclassify. Finish with k * k * k == target.

Overflow

Prefer long

Cubing an int near the edge can overflow. Multiply in long.

Time and Space Complexity

ProgramTimeExtra space
Integer scanO(∛|n|)O(1)
Math.cbrt + verifyO(1)O(1)

Key Takeaways

  • Definition: n is a cube when n = k³ for an integer k.
  • Verify in integers: never stop at a floating-point guess.
  • Two options: scan k, or round Math.cbrt and re-check.
  • Test set: 0, 8, 27, −8 true; 10 false.

One line: find integer k such that k × k × k equals n.

Frequently Asked Questions

An integer n is a perfect cube when n = k³ for some integer k. Examples: 0 = 0³, 1 = 1³, 8 = 2³, 27 = 3³, and −8 = (−2)³.
Search for an integer k such that k * k * k equals n, or estimate the cube root with Math.cbrt and verify the cube in integer arithmetic.
Yes. 0 = 0³, so it is a perfect cube.
Yes. Cubing preserves sign, so −8 = (−2)³ is a perfect cube.
An integer scan up to the cube root is O(∛|n|). Rounding Math.cbrt and verifying is effectively O(1) for fixed-size integers.

Next: Decimal to Binary

Convert a base-10 integer into a binary string.

Decimal to binary tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

18 people found this page helpful