Java Program to Find Cube of a Number

In this program, we will learn to code the Java Program to Find Cube of a Number. Let’s understand How to calculate cube of a number in Java Programming Language. In the previous program, we have learned to code the Java Program to Find Square of a Number.

We are going to solve this Java Program to Find Cube of a Number using two approaches as follow

  • Using Math.pow() fuction
  • Using “*” operator

Now, let’s get straight into the code of the Java Program to Find Cube of a Number.

Java Program to Find Cube of a Number

Using Math.pow Function

Let’s see the code of the Java Program to Find Cube of a Number using Math.pow() function.

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args)
	{
		    Scanner sc = new Scanner(System.in);
		    System.out.println("Enter the number: ");
		    int num = sc.nextInt();
		    
		    //Calculating the cube of the number
		    int cube = (int) Math.pow(num,3);
		    
		    System.out.println("Cube of the number "+num+" is "+cube+" .");
		 
	}
}

Output

Enter the number: 7
Cube of the number 7 is 343 .

How Does This Program Work ?

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number : ");
        int num = sc.nextInt();

In this program, we have taken the input of the number we want to calculate the cube of using the Scanner class in Java.

                    //Calculating the cube of the number
		    int cube = (int) Math.pow(num,3);

Then, we have used the Math.pow() function in Java to calculate the cube of the number. The Math.pow() function returns a double value, so, to store the number in int type we have explicitly typecasted it.

		    System.out.println("Cube of the number "+num+" is "+cube+" .");

Then, we displayed the result.

This is the Java Program to Find Cube of a Number using Math.pow() function. Now, let’s see another approach using “*” operator.

Java Program to Find Cube of a Number

Using “*” Operator

Let’s see the code of the Java Program to Find Cube of a Number using “*” operator.

import java.util.*;
import java.lang.*;

class Main
{
	public static void main (String[] args)
	{
		    Scanner sc = new Scanner(System.in);
		    System.out.println("Enter the number: ");
		    int num = sc.nextInt();
		    
		    //Calculating the cube of the number
		    int cube = num * num * num;
		    
		    System.out.println("Cube of the number "+num+" is "+cube+" .");
		 
	}
}

Output

Enter the number: 11
Cube of the number 11 is 1331 .

This is the Java Program to Find Cube of a Number using “*” operator.

Conclusion

I hope after going through this post, you understand the Java Program to Find Cube of a Number. In this programming example, we have discussed two approaches to find the cube of the number in Java Programming Language.
If you have any doubt regarding the topic, feel free to contact us in the comment section. We will be delighted to help you.

Also Read:

Leave a Comment

Your email address will not be published. Required fields are marked *