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

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

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

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

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

(a-b)2+(b-c)2+(c-a)2  = 2a2 + 2b2 + 2c2 -2ab – 2bc -2ca

Now we will convert this into a valid Java expression.

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

=  2a2 + 2b2 + 2c2 -2ab – 2bc -2ca

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

Example:

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

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

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

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

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

Method-2: Java Program to Compute (a-b)2+(b-c)2+(c-a)2 where Value of a, b, and c are Given By 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)2+(b-c)2+(c-a)2  
  • 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 double variable and assigned the formulated value to it.
        double d=2*Math.pow(a,2)+ 2*Math.pow(b,2)+ 2*Math.pow(c,2)-2*(a*b) -2*(b*c) -2*(c*a);
        System.out.println("(a-b)2+(b-c)2+(c-a)2= "+d);
    }
}
Output:

a= 23
b= 45
c= 34
(a-b)2+(b-c)2+(c-a)2= 726.0

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

a= 56
b= 78
c= 90
(a-b)2+(b-c)2+(c-a)2= 1784

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: