Java Program to Calculate the Cube of a Number

In this tutorial we will learn writing java program to calculate the cube of a given number.  Our program will take a number and will return the cube of that number as output.

What is Cube of Number?

When a number is multiplied to itself three times, resultant number is known as cube of number.

For example:

Suppose we want to calculate the cube of 5

Cube of 5 = 5^2 =  5*5 = 25

Program 1: Java Program to Calculate Cube of number

This is a simple and straightforward approach. Here we are taking the input number from the user and calculating the cube using a simple mathematical expression (num * num * num). And after calculating the cube, we are printing the result.

import java.util.*;
public class Main
{
   	public static void main(String[] args) {
            double num,cube;
		    Scanner sc = new Scanner(System.in);
	      	System.out.println("Please give the number to calculate cube");
	      	num= sc.nextDouble();
	      	cube = num*num*num; 
	      	System.out.println("Cube of "+num+" is ");
    		System.out.printf("%.2f",cube);
	}
}

Output

Please give the number to calculate cube
5
Cube of 5.0 is 
125.00

Program 2: Calculate Cube using Java 8

In this program we are using the Java 8 concepts. This is less conventional approach. This program we are writing just to let you know the java 8 concepts and approach. And how to can use java 8 to solve the problem.

import java.util.*;
import java.util.stream.*;
public class Main
{
   	public static void main(String[] args) {
            int num;
            double cube;
		    Scanner sc = new Scanner(System.in);
	      	System.out.println("Please give the number to calculate cube");
	      	num = sc.nextInt();
	      	cube = IntStream.of(num)
                .map(n -> n * n * n)
                .findFirst()
                .orElse(0);
	      	System.out.println("Cube of "+num+" is ");
    		System.out.printf("%.2f",cube);
	}
}

Output

Please give the number to calculate cube
5
Cube of 5 is 
125.00

Conclusion

In this tutorial you learnt writing a Java program to calculate the cube of a given number. The cube of a number is obtained by multiplying the number by itself three times. There are two methods we have shown to calculate the cube.

Program 1: Java Program to Calculate Cube of a Number

This was simple and straightforward approach. Here we have taken the input number from the user and calculating the cube using a simple mathematical expression (num * num * num). Then result was printed to the console.

Program 2: Calculate Cube using Java 8

In this approach we are using the Java 8 concepts. This is less conventional approach. This program we have just written to let you know the java 8 concepts and approach.

Through these examples, you gained insights into different ways of calculating the cube of a number in Java. Whether using a traditional mathematical expression or exploring less common approaches with Java 8 streams, you now have the knowledge to choose the method that best suits your coding preferences and requirements.

Happy coding!