How to cube in java – Java Program to Find Cube of a Number

How to cube in java: In the previous article, we have seen Java Program to Check if Number is Divisible 5 and 11

In this article we will see how to find cube of a number using Java programming language.

Java Program to Find Cube of a Number

Cube of a number represents multiplying the number with itself for 3 times.

For example

Cube of 3 = (3*3*3) = 27

Cube of 15 = ( 15*15*15) = 3375

Let’s see different ways to find cube of a number.

Method-1: Java Program to Find Cube of a Number By Using Static Input Value

Approach:

  • Declare an integer variable say ‘number‘ and initialize a value to it.
  • Find cube by multiplying the number with itself for 3 times.
  • Print the result.

Program:

public class Main 
{
   public static void main(String args[])
   {
      //a number declared
      int number = 5;
      //finding cube of number by multiplying it 3 times with itself
      //and printing result
      System.out.println("Cube of number: "+ (number*number*number));
   }
}
Output:

Cube of number: 125

Method-2: Java Program to Find Cube of a Number By Using User Input Value

Approach:

  • Declare an integer variable say ‘number‘ and take the value as user input by using Scanner class.
  • Find cube by multiplying the number with itself for 3 times.
  • Print the result.

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      
      //taking a number input from user
      System.out.println("Enter a number: ");
      int number = sc.nextInt();
      //finding cube of number by multiplying it 3 times with itself
      //and printing result
      System.out.println("Cube of number: "+ (number*number*number));
   }
}
Output:

Enter a number: 
15
Cube of number: 3375

Method-3: Java Program to Find Cube of a Number By Using User Defined Method

Approach:

  • Declare an integer variable say ‘number‘ and take the value as user input by using Scanner class.
  • Call user defined method findCube() and pass the number as parameter.
  • Inside method find cube by multiplying the number with itself for 3 times.
  • Print the result.

Program:

import java.util.*;

public class Main 
{
   public static void main(String args[])
   {
      Scanner sc=new Scanner(System.in);
      
      //taking a number input from user
      System.out.println("Enter a number: ");
      int number = sc.nextInt();
      //calling user defined method findCube()
      findCube(number);
   }
   
   //findCube() method to find cube of a number
   public static void findCube(int number)
   {
      //finding cube of number by multiplying it 3 times with itself
      int cube = number*number*number;
      //printing result
      System.out.println("Cube of number: "+ cube);
   }
}
Output:

Enter a number: 
6
Cube of number: 216

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Related Java Programs: