JS Topics
- JS Intro
- JS File Handling
- JS Array Methods
- JS Console Methods
- JS Date Methods
- JS Navigator Methods
- JS Node Methods
- JS Number Methods
- JS String Properties
- JS String Methods
- JS Window Methods
- JS Cookies
- JS Interview Programs
- Abundant Number
- Amicable Number
- Armstrong Number
- Average of N Numbers
- Automorphic Number
- Biggest of three numbers
- Binary to Decimal
- Common Divisors
- Composite Number
- Condense a Number
- Cube Number
- Decimal to Binary
- Decimal to Octal
- Disarium Number
- Even Number
- Evil Number
- Factorial of a Number
- Fibonacci Series
- GCD
- Happy Number
- Harshad Number
- LCM
- Leap Year
- Magic Number
- Matrix Addition
- Matrix Division
- Matrix Multiplication
- Matrix Subtraction
- Matrix Transpose
- Maximum Value of an Array
- Minimum Value of an Array
- Multiplication Table
- Natural Number
- Number Combination
- Odd Number
- Palindrome Number
- Pascalβs Triangle
- Perfect Number
- Perfect Square
- Power of 2
- Power of 3
- Pronic Number
- Prime Factor
- Prime Number
- Smith Number
- Strong Number
- Sum of Array
- Sum of Digits
- Swap Two Numbers
- Triangular Number
- JS Star Pattern
- JS Number Pattern
- JS Alphabet Pattern
JavaScript 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 JavaScript program that efficiently checks whether a given number is a perfect cube or not.
π Example
Let's take a look at the JavaScript code that accomplishes this functionality.
// Function to check if a number is a perfect cube
function isPerfectCube(number) {
// Calculate the cube root of the number
const 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
// Replace this value with your desired number
const number = 27;
// Check if the number is a perfect cube
if (isPerfectCube(number)) {
console.log(`${number} is a perfect cube.`);
} else {
console.log(`${number} is not a perfect cube.`);
}
π» Testing the Program
To test the program with different numbers, simply replace the value of the number variable.
27 is a perfect cube.
Run the script to check if the number is a perfect cube.
π§ How the Program Works
- The program defines a function isPerfectCube that takes a number as input and returns true if it is a perfect cube, and false otherwise.
- Inside the function, it calculates the cube root of the number using the Math.cbrt function.
- It then checks if the cube root is close to an integer using a tolerance value (1e-10 in this case).
- The program initializes a variable with a number and calls the isPerfectCube function to determine if it's a perfect cube.
π Between the Given Range
Let's take a look at the JavaScript code that identifies and displays cube numbers in the range from 1 to 50.
// Function to check if a number is a cube number
function isCubeNumber(num) {
// Calculate the cube root of the number
let cubeRoot = Math.cbrt(num);
// Check if the cube root is an integer
return cubeRoot % 1 === 0;
}
// Function to find and display cube numbers in a specified range
function displayCubeNumbersInRange(start, end) {
console.log(`Cube numbers in the range ${start} to ${end}:`);
for (let i = start; i <= end; i++) {
if (isCubeNumber(i)) {
console.log(i);
}
}
}
// Define the range
const startRange = 1;
const endRange = 50;
// Call the function to display cube numbers
displayCubeNumbersInRange(startRange, endRange);
π» Testing the Program
Cube numbers in the range 1 to 50: 1 8 27
Run the script to see the cube numbers in the range from 1 to 50.
π§ How the Program Works
- The program defines a function isCubeNumber that checks if a given number is a cube number by calculating its cube root and verifying if it's an integer.
- Another function, displayCubeNumbersInRange, iterates through the specified range (1 to 50) and prints cube numbers using the isCubeNumber function.
- The range is set from 1 to 50, as specified.
π§ 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:
Author
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
If you have any doubts regarding this article (JavaScript Program to Check for a Cube Number), please comment here. I will help you immediately.