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

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

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

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

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

(a+b)(b+c)(c+a) = ab(a+b) + bc(b+c) + ca(c+a) + 2abc

Now we will convert this into a valid Java expression.

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

= ab(a+b) + bc(b+c) + ca(c+a) + 2abc

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

Example:

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

(a+b)(b+c)(c+a) = a*b*(a+b) + b*c*(b+c) + c*a*(c+a) + 2*a*b*c
= 4*2*(4+2) + 2*3*(2+3) + 3*4*(3+4) + 2*4*2*3
= 210

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

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

Approach:

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

Program:

class Main
{
    public static void main(String[] args)
    {
        //declare the first integer variable with an integer value
        int a= 4;
        System.out.println("a= " +a);
        //declare the second integer variable with an integer value
        int b= 2;
        System.out.println("b= "+b);
        //declare the second integer variable with an integer value
        int c= 3;
        System.out.println("c= "+c);

        //declare another integer variable and assigned the formulated value to it.
        int d= a*b*(a+b) + b*c*(b+c) + c*a*(c+a) + 2*a*b*c;
        System.out.println("(a+b)(b+c)(c+a)= "+d);
    }
}
Output:

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

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

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)(b+c)(c+a)
  • 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();
        //declare another integer variable and assigned the formulated value to it.
        int d= a*b*(a+b) + b*c*(b+c) + c*a*(c+a) + 2*a*b*c;
        System.out.println("(a+b)(b+c)(c+a)= "+d);
    }
}
Output:

a= 8
b= 9
c= 10
(a+b)(b+c)(c+a)= 5814

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

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 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)(b+c)(c+a)
  • 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 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= a*b*(a+b) + b*c*(b+c) + c*a*(c+a) + 2*a*b*c;
        System.out.println("(a+b)(b+c)(c+a)= "+d);
    }
}
Output:

a= 5
b= 6
c= 7
(a+b)(b+c)(c+a)= 1716

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: