Kodekraftt
/Blog
/Java Program To Check Whether A Number Is A Perfect Cube Without Using Mathcbrt
Join Early AccessContact UsPrivacy Policy
Java BasicsOOPsDSA with JavaQuizzesInterview Preparation

© 2026 KodeKraftt. All rights reserved.

Build smarter. Learn more. Innovate better.

Java Program to Check Whether a Number Is a Perfect Cube Without Using math.cbrt()

Learn how to determine if a number is a perfect cube in Java without using the built-in math.cbrt() function. This method uses simple iteration and works for both positive and negative numbers.

Java Program to Check Whether a Number Is a Perfect Cube Without Using math.cbrt()

A number is called a perfect cube if it can be expressed as the cube of an integer. For example, 1, 8, 27, 64, 125, 216, etc., are perfect cubes.

In this blog, we will write a Java program to check whether a number is a perfect cube without using the Math.cbrt() function. Instead, we will use a simple loop to find the integer whose cube matches the given number.

Steps to Check Perfect Cube:

  • Take the number as input.
  • Iterate from 1 up to the absolute value of the number.
  • Check if i * i * i equals the number.
  • If yes, it is a perfect cube; otherwise, it is not.

Java Program:


import java.util.Scanner;

public class PerfectCubeWithoutCbrt {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a number: ");
        int num = sc.nextInt();

        boolean isCube = false;

        for (int i = 1; i <= Math.abs(num); i++) {
            if (i * i * i == num || -i * i * i == num) {
                isCube = true;
                break;
            }
        }

        if (isCube) {
            System.out.println(num + " is a perfect cube.");
        } else {
            System.out.println(num + " is NOT a perfect cube.");
        }
    }
}

Example Output:


Enter a number: 64
64 is a perfect cube.

Enter a number: 50
50 is NOT a perfect cube.

This approach is simple, effective, and works for both positive and negative numbers without relying on Java's built-in cube root functions.

You might also like

Check if Two Strings Are Rotations of Each Other in Java — Definition, Examples and Program

Learn how to check whether two strings are rotations of each other in Java using logical string comparison without using complex libraries.

Find the Shortest Word in a Sentence in Java — Definition, Examples and Program

Learn how to find the shortest word in a sentence in Java using a simple character-by-character approach without using split() or advanced string methods.

Abundant Number in Java — Definition, Examples and Program

Learn what an abundant number is in Java with definition, examples, and a clean program to check whether a number is abundant or not.

Concatenate Two Strings in Java Without Using + Operator — Examples and Program

Learn how to concatenate two strings in Java without using the + operator. This blog explains the logic with examples, a Java program, and practice challenges.

Check if a String is Empty or Null in Java — Definition, Examples and Program

Learn how to check whether a string is null or empty in Java. This blog explains the difference, common mistakes, examples, and a safe Java program with practice challenges.

Convert String to Uppercase in Java Without Using String Library

Learn how to convert a string to uppercase in Java without using built-in String methods. This blog explains the ASCII-based logic with examples, a Java program, and practice challenges.