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

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

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

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

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

ab(a-b)+bc(b-c)+ca(c-a) = (a-b)(b-c)(a-c)

Now we will convert this into a valid Java expression.

Let x= ab(a-b)+bc(b-c)+ca(c-a)

=  (a-b)(b-c)(a-c)

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

Example:

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

ab(a-b)+bc(b-c)+ca(c-a)
= (a-b)*(b-c)*(a-c)
= (2-4)*(2-3)*(3-4)
= -2

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

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

Approach:

  • Declare and initialize three integer variables say a, b and c
  • By using the formula compute ab(a-b)+bc(b-c)+ca(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 assign the formulated value to it.
        int d=(a-b)*(b-c)*(a-c);
        System.out.println("ab(a-b)+bc(b-c)+ca(c-a)= "+d);
    }
}
Output:

a= 4
b= 2
c= 3
ab(a-b)+bc(b-c)+ca(c-a)= -2

Method-2: Java Program to Compute ab(a-b)+bc(b-c)+ca(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
  • Prompt the user to enter the values to the corresponding variables.
  • Then by using the formula compute ab(a-b)+bc(b-c)+ca(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 first input from the user.
        int c= sc.nextInt();
        //declare another integer variable and assign the formulated value to it.
        int d=(a-b)*(b-c)*(a-c);
        System.out.println("ab(a-b)+bc(b-c)+ca(c-a)= "+d);
    }
}
Output:

a= 23
b= 12
c= 9
ab(a-b)+bc(b-c)+ca(c-a)= 462

Method-3: Java Program to Compute ab(a-b)+bc(b-c)+ca(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
  • Prompt the user to enter the values to the corresponding variables.
  • Then call the user defined method say computeValue() and pass a, b and c as parameter.
  • Then inside that method by using the formula compute ab(a-b)+bc(b-c)+ca(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 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 assign the formulated value to it.
        int d=(a-b)*(b-c)*(a-c);
        System.out.println("ab(a-b)+bc(b-c)+ca(c-a)= "+d);
    }
}
Output:

a= 234
b= 124
c= 178
ab(a-b)+bc(b-c)+ca(c-a)= -332640

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Related Java Programs: