Kodekraftt
/Blog
/Perfect Cube In Java With Explanation Definition Examples And Program
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Perfect Cube in Java with Explanation, Definition, Examples, and Program

A Perfect Cube is a number that can be expressed as the cube of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect cube.

Perfect Cube in Java

A Perfect Cube is a number that can be represented as the cube of an integer. In simple terms, a number N is a Perfect Cube if:

N = a³, for some integer a

For example, 8 = 2³ and 27 = 3³, so both are perfect cubes.


Examples

Example 1: 27

  • Cube root of 27 = 3
  • 3 × 3 × 3 = 27
  • So, 27 is a Perfect Cube.

Example 2: 30

  • Cube root of 30 ≈ 3.107...
  • Not an integer
  • So, 30 is NOT a Perfect Cube.

Java Program to Check Perfect Cube


public class PerfectCube {

    // Function to check if a number is a perfect cube
    static boolean isPerfectCube(int num) {
        int cubeRoot = (int) Math.round(Math.cbrt(num));
        return cubeRoot * cubeRoot * cubeRoot == num;
    }

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

        if (isPerfectCube(num)) {
            System.out.println(num + " is a Perfect Cube");
        } else {
            System.out.println(num + " is NOT a Perfect Cube");
        }
    }
}

How the Program Works

We use Math.cbrt() to calculate the cube root of the number. Then we round it to the nearest integer and check if its cube equals the original number.


Practice Challenges

  1. Print all perfect cubes between 1 and 1000.
  2. Write a program to find the cube root of a number without using inbuilt functions.
  3. Given an array of numbers, print only those which are perfect cubes.

You might also like

Perfect Square in Java with Explanation, Definition, Examples, and Program

A Perfect Square is a number that can be expressed as the square of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect square.

Fascinating Number in Java with Explanation, Definition, Examples, and Program

A Fascinating Number is a number which, when multiplied by 2 and 3 and then concatenated with itself, forms a string that contains all digits from 1 to 9 exactly once.

Check Perfect Square in Java Without Using Math.sqrt() – Explanation, Logic, Examples, and Program

Learn how to check whether a number is a Perfect Square in Java without using Math.sqrt(). This guide explains the logic, provides examples, and includes an efficient program using iterative checking.

Find Square Root of a Number in Java Without Using Math.sqrt() – Explanation, Logic, Examples, and Program

Learn how to calculate the square root of any number in Java without using Math.sqrt(). This guide explains the binary search method, includes examples, and provides a clean Java program.

Perfect Square in Java with Explanation, Definition, Examples, and Program

A Perfect Square is a number that can be expressed as the square of an integer. This guide explains the concept with examples and provides a simple Java program to check if a number is a perfect square.

LCM of Two Numbers in Java with Explanation, Formula, Examples, and Program

The LCM of two numbers is the smallest number that is perfectly divisible by both. This article explains the concept with examples and provides a simple and efficient Java program.