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

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

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

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

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

(a+b+c)(b+c-a)(c+a-b)(a+b-c) = 2a2b2 + 2b2c2 + 2c2a2 – a4 – b4 – c4

Now we will convert this into a valid Java expression.

Let x= (a+b+c)(b+c-a)(c+a-b)(a+b-c)

= 2a2b2 + 2b2c2 + 2c2a2 – a4 – b4 – c4

=  (2*a*a*b*b)+( 2*b*b*c*c)+( 2*c*c*a*a)-(a*a*a*a)-(b*b*b*b)-(c*c*c*c)

Or, (2*Math.pow(a,2)* Math.pow(b,2)) + (2*Math.pow(b,2)* Math.pow(c,2)) +   (2*Math.pow(c,2)* Math.pow(a,2)) – (Math.pow(a,4)) – (Math.pow(b,4)) – (Math.pow(c,4))

Example:

Suppose a=4, b=2 and c= 3, Then

(a+b+c)(b+c-a)(c+a-b)(a+b-c)
= (2*a*a*b*b)+( 2*b*b*c*c)+( 2*c*c*a*a)-(a*a*a*a)-(b*b*b*b)-(c*c*c*c)
= (2*4*4*2*2)+( 2*2*2*3*3)+( 2*3*3*4*4)-(4*4*4*4)-(2*2*2*2)-(3*3*3*3)
= 135

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

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

Approach:

  • Declare and initialize two integer variables say a, b, and c.
  • By using the formula compute (a+b+c)(b+c-a)(c+a-b)(a+b-c)
  • 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= 2;
        System.out.println("b= "+b);
        //declare the second integer variable with a integer value
        int c= 3;
        System.out.println("c= "+c);
        //declare another integer variable and assigned the formulated value to it.
        int d= (2*a*a*b*b)+( 2*b*b*c*c)+( 2*c*c*a*a)-(a*a*a*a)-(b*b*b*b)-(c*c*c*c);
        System.out.println("(a+b+c)(b+c-a)(c+a-b)(a+b-c)= "+d);
    }
}
Output:

a= 4
b= 2
c= 3
(a+b+c)(b+c-a)(c+a-b)(a+b-c)= 135

Method-2: Java Program to Compute (a+b+c)(b+c-a)(c+a-b)(a+b-c) where Value of a, b, and c are Given y Using User Input Value and Pow()

Approach:

  • Declare three integer variables say a, b, and c.
  • Take the value of a, b, and c as user input by using Scanner class.
  • Then By using the formula compute (a+b+c)(b+c-a)(c+a-b)(a+b-c)
  • 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 first input from the user.
        int c= sc.nextInt();
        //declare another integer variable and assigned the formulated value to it.
        double d= (2*Math.pow(a,2)* Math.pow(b,2)) + (2*Math.pow(b,2)* Math.pow(c,2)) +   (2*Math.pow(c,2)* Math.pow(a,2)) - (Math.pow(a,4)) - (Math.pow(b,4)) - (Math.pow(c,4));
        System.out.println("(a+b+c)(b+c-a)(c+a-b)(a+b-c)= "+(int)d);
    }
}
Output:

a= 34
b= 45
c= 56
(a+b+c)(b+c-a)(c+a-b)(a+b-c)= 9361575

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

Approach:

  • Declare two integer variables say a, b, and c
  • Take the value of a, b, and c 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)(b+c-a)(c+a-b)(a+b-c)
  • 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 first input from the user.
        int c= sc.nextInt();
        //call the function
        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= (2*a*a*b*b)+( 2*b*b*c*c)+( 2*c*c*a*a)-(a*a*a*a)-(b*b*b*b)-(c*c*c*c);
        System.out.println("(a+b+c)(b+c-a)(c+a-b)(a+b-c)= "+d);
    }
}
Output:

a= 12
b= 34
c= 32
(a+b+c)(b+c-a)(c+a-b)(a+b-c)= 589680

Want to excel in java coding? Practice with these Java Programs examples with output and write any
kind of easy or difficult programs in the java language.

Related Java Programs: