Java Programs

Java Program to Calculate Cube of Number

In this tutorial, we are going to 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 an output.

What is cube of Number?

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

For example

Suppose we want to calculate the cube of 5

Cube of 5 = 5^3 = 5*5*5 = 125

Java Program to calculate cube of a number

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

Explanation

For input 5.0, the output generated by our program is 5*5*5 i.e. equals 125.0.

About the Author: Elavarasan PK

Technical Specialist, Intersoft Data Labs