# Java Program to Compute (a+b+c)3 where Value of a, b and c are Given

In the previous article we have discussed Java Program to Compute (a-b-c)2 where Value of a, b and c are Given

In this program we are going to see how to compute (a+b+c)3 where value of a, b and c are given by using Java programming language.

## Java Program to Compute (a+b+c)3 where Value of a, b and c are Given

The formula of (a + b + c)3 is given below.

(a + b +c)3 = a3 + b3 + c3 + 3(a + b)(b + c)(c + a)

Now we will convert this into a valid Java expression.

Let x = (a+b+c)3

=  a3 + b3 + c3 + 3(a + b)(b + c)(c + a)

=  a3 + b3 + c3+3a2b+3b2a + 3b2c+3c2b  +3c2a +3a2c+6abc

=(a*a*a) + (b*b*b) + (c*c*c) + (3*a*a*b) + (3*b*b*a) + (3*b*b*c) + (3*c*c*b) + (3*c*c*a) + (3*a*a*c) + (6*a*b*c)

Example:

Suppose a=2, b=2, and c=2, then

(a+b+c)3
= (a*a*a) + (b*b*b) + (c*c*c) + (3*a*a*b) + (3*b*b*a) + (3*b*b*c) + (3*c*c*b) + (3*c*c*a) + (3*a*a*c) + (6*a*b*c)
= (2*2*2) + (2*2*2) +(2*2*2)+ (3*2*2*2) + (3*2*2*2) + (3*2*2*2) + (3*2*2*2) + (3*2*2*2) + (3*2*2*2) + (6*2*2*2)

Now let’s see different ways to compute (a+b+c)3

### Method-1: Java Program to Compute (a+b+c)3 where Value of a, b and c are Given By Using Static Input Value

Approach:

• Declare and initialize three integer variables say ab and c.
• By using the formula compute (a+b+c)3
• Print the result.

Program:

class Main
{
public static void main(String[] args)
{
//declare the first integer variable with a integer value
int a= 4;
System.out.println("a= " +a);
//declare the second integer variable with a integer value
int b= 4;
System.out.println("b= "+b);
//declare the third integer variable with a integer value
int c= 2;
System.out.println("c= "+c);
//declare another integer variable and assigned the formulated value to it.
int d=(a*a*a) + (b*b*b) + (c*c*c) + (3*a*a*b) + (3*b*b*a) + (3*b*b*c) + (3*c*c*b) + (3*c*c*a) + (3*a*a*c) + (6*a*b*c);
System.out.println("(a+b+c)3= "+d);
}
}

Output:

a= 4
b= 4
c= 2
(a+b+c)3= 1000

### Method-2: Java Program to Compute (a+b+c)3 where Value of a, b and c are Given By Using pow() Function

Approach:

• Declare three integer variables say ab and c.
• Take the value of a and b as user input by using Scanner class.
• Then By using the formula compute (a+b+c)3
• Print the result.

Program:

import java.lang.Math;
import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
//create object of scanner class.
Scanner sc=new Scanner(System.in);
System.out.print("a= ");
//Take the first input from the user.
int a= sc.nextInt();
System.out.print("b= ");
//Take the second input from the user.
int b= sc.nextInt();
System.out.print("c= ");
//Take the third input from the user.
int c= sc.nextInt();
//declare another integer variable and assigned the formulated value to it.
double  d=(Math.pow(a,3)) + (Math.pow(b,3)) + (Math.pow(c,3)) + (3* Math.pow(a,2)*b) + (3* Math.pow(b,2)*a) + (3* Math.pow(b,2)*c) + (3* Math.pow(c,2)*b) + (3* Math.pow(c,2)*a) + (3* Math.pow(a,2)*c) + (6*a*b*c);
System.out.println("(a+b+c)3= "+(int)d);
}
}

Output:

a= 4
b= 5
c= 6
(a+b+c)3= 3375

### Method-3: Java Program to Compute (a+b+c)3 where Value of a, b and c are Given By Using User Defined Method

Approach:

• Declare three integer variables say ab and c.
• Take the value of a and b as user input by using Scanner class.
• Then call a user defined method say computeValue() and pass a, b and c as parameter.
• Then inside method by using the formula compute (a+b+c)3
• Print the result.

Program:

import java.util.Scanner;
class Main
{
public static void main(String[] args)
{
//create object of scanner class.
Scanner sc=new Scanner(System.in);
System.out.print("a= ");
//Take the first input from the user.
int a= sc.nextInt();
System.out.print("b= ");
//Take the second input from the user.
int b= sc.nextInt();
System.out.print("c= ");
//Take the third input from the user.
int c= sc.nextInt();
//call the funtion
computeValue(a,b,c);
}
//define the method
public static void computeValue(int a, int b,int c)
{
//declare another integer variable and assigned the formulated value to it.
int d=(a*a*a) + (b*b*b) + (c*c*c) + (3*a*a*b) + (3*b*b*a) + (3*b*b*c) + (3*c*c*b) + (3*c*c*a) + (3*a*a*c) + (6*a*b*c);
System.out.println("(a+b+c)3= "+d);
}
}

Output:

a= 8
b= 10
c= 12
(a+b+c)3= 27000

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Related Java Programs: