Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

Java Program to Check for a Cube Number

Posted in Java Tutorial
Updated on Nov 20, 2024
By Mari Selvan
πŸ‘οΈ 79 - Views
⏳ 4 mins
πŸ’¬ 1 Comment
Java Program to Check for a Cube Number

Photo Credit to CodeToFun

πŸ™‹ Introduction

In the realm of programming, mathematical properties of numbers often lead to interesting and practical applications. One such property is being a cube number. A cube number is an integer that is the cube of an integer.

In this tutorial, we'll delve into a Java program that efficiently checks whether a given number is a perfect cube or not.

πŸ“„ Example

Let's take a look at the Java code that accomplishes this functionality.

CubeNumberChecker.java
Copied
Copy To Clipboard
public class CubeNumberChecker {

// Function to check if a number is a perfect cube
static boolean isPerfectCube(int number) {
  // Calculate the cube root of the number
  double cubeRoot = Math.cbrt(number);

  // Check if the cube root is close to an integer
  return Math.abs(cubeRoot - Math.round(cubeRoot)) < 1e-10;
}

// Driver program
public static void main(String[] args) {
  // Replace this value with your desired number
  int number = 27;

  // Check if the number is a perfect cube
  if (isPerfectCube(number)) {
    System.out.println(number + " is a perfect cube.");
  } else {
    System.out.println(number + " is not a perfect cube.");
  }
}
}

πŸ’» Testing the Program

To test the program with different numbers, simply replace the value of the number variable in the main method.

Output
27 is a perfect cube number.

Compile and run the program to check if the number is a perfect cube.

🧠 How the Program Works

  1. The program defines a class CubeNumberChecker containing a static method isPerfectCube that takes an integer as input and returns true if it is a perfect cube, and false otherwise.
  2. Inside the method, it calculates the cube root of the number using Math.cbrt.
  3. It then checks if the cube root is close to an integer using a tolerance value (1e-10 in this case).
  4. The main method initializes a variable with a number and calls the isPerfectCube method to determine if it's a perfect cube.

πŸ“ Between the Given Range

Let's delve into the Java code that checks and displays cube numbers in the specified range.

isCubeNumber.java
Copied
Copy To Clipboard
public class CubeNumbers {

  // Function to check if a number is a cube number
  static boolean isCubeNumber(int num) {
    int cubeRoot = (int) Math.round(Math.pow(num, 1.0 / 3));
    return cubeRoot * cubeRoot * cubeRoot == num;
  }

  // Driver program
  public static void main(String[] args) {
    System.out.println("Cube numbers in the range 1 to 50:");

    // Iterate through the range and check for cube numbers
    for (int i = 1; i <= 50; i++) {
      if (isCubeNumber(i)) {
        System.out.print(i + " ");
      }
    }
  }
}

πŸ’» Testing the Program

Output
Cube numbers in the range 1 to 50:
1 8 27

Compile and run the program to see the cube numbers in the specified range.

🧠 How the Program Works

  1. The program defines a class CubeNumbers containing a static method isCubeNumber that checks if a given number is a cube number.
  2. Inside the method, it calculates the cube root of the number and checks if the cube of the rounded cube root equals the original number.
  3. The main function iterates through the range of 1 to 50 and prints the cube numbers.

🧐 Understanding the Concept of Cube Number

A cube number is the result of raising an integer to the power of 3. For example, 2 * 2 * 2 = 8, so 8 is a cube number.

🎒 Optimizing the Program

The provided program is straightforward, but you might consider optimizing it further. For instance, you can explore alternative methods for checking if a number is a cube number without using the cbrt function.

Feel free to incorporate and modify this code as needed for your specific use case. Happy coding!

πŸ‘¨β€πŸ’» Join our Community:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
πŸ‘‹ Hey, I'm Mari Selvan

For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.

Buy me a coffee to make codetofun.com free for everyone.

Buy me a Coffee

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
11 months ago

If you have any doubts regarding this article (Java Program to Check for a Cube Number), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy