How to cube a number in java: In the previous article, we have discussed Java Program to Check Two Numbers are Amicable Numbers or Not
In this article we are going to understand what Cube number is and how we can check whether a number is Cube or not in Java with examples.
Program to Check Cube Number
Cube numbers are numbers that are formed by multiplying a number by itself two times i.e. raised to the power 3.
Example : 729: 9*9*9 Cube Number 343: 7*7*7 Cube Number 81: Not a cube number
In the above examples the numbers 343 and 729 are Cube numbers as they are the products of 7 and 9 raised to the power 3. Where, 81 is not a cube.
Let’s see different ways to check
Provided list of Simple Java Programs is specially designed for freshers and beginners to get familiarize with the concepts of Java programming language and become pro in coding.
Approach :
- Enter a number/declare a number and store it.
- We calculate the roots of the number.
- We check if the roots multiplied to itself twice is equivalent as the entered number, then the number is said to be a Cube number.
Method-1: Java Program to Check Cube Number By Using Static Value
import java.util.Scanner; public class CubeNumber { public static void main(String args[]) { //A number declared int num = 81; // Finding out the cube root by raising the number to the power 1/3 // And then rounding it to the nearest integer int root = (int)Math.round(Math.pow(num,1.0/3.0)); // Checking whether the root when multiplies to itself twice, gives the same number if(root*root*root==num) { System.out.println(num+" is a cube"); } else { System.out.println(num+" is not a cube"); } } }
Output: 729 is a cube
Method-2: Java Program to Check Cube Number By User Input Value
import java.util.Scanner; public class CubeNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); // Finding out the cube root by raising the number to the power 1/3 // And then rounding it to the nearest integer int root = (int)Math.round(Math.pow(num,1.0/3.0)); // Checking whether the root when multiplies to itself twice, gives the same number if(root*root*root==num) { System.out.println(num+" is a cube"); } else { System.out.println(num+" is not a cube"); } } }
Output: Case-1 Enter a number : 27 27 is a cube Case-2 Enter a number : 81 81 is a cube
Method-3: Java Program to Check Cube Number By User Defined Method
import java.util.Scanner; public class CubeNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); //checkNumber() method called to checkcube number checkNumber(num); } //user defined method to check Cube number public static void checkNumber(int num) { // Finding out the cube root by raising the number to the power 1/3 // And then rounding it to the nearest integer int root = (int)Math.round(Math.pow(num,1.0/3.0)); // Checking whether the root when multiplies to itself twice, gives the same number if(root*root*root==num) { System.out.println(num+" is a cube"); } else { System.out.println(num+" is not a cube"); } } }
Output: Case-1 Enter a number : 27 27 is a cube Case-2 Enter a number : 81 81 is a cube
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Related Java Programs: