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

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

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

Some of the example to how to compute the values of a and b are:

  • A B Java
  • A^B In Java
  • Java A B
  • A B In Java
  • Java Ab
  • (A+B)^2-(A-B)^2
  • A 2 B 2 Formula
  • (A-B)(A+B)
  • A & B In Java
  • A%B In Java
  • A-B 2
  • (A +B)^2
  • (A-B)2
  • A%B Java
  • A^B Java
  • (A+B)2-(A-B)2
  • (A+B)^2+(A-B)^2
  • (A+B)2 + (A-B)2
  • A+B)^2+(A-B)^2
  • (A+B)2+(A-B)2
  • (A+B)^2 + (A-B)^2

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

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

(a+b)2+(a-b)2  =  2(a2 + b2)

Now we will convert this into a valid Java expression.

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

=  2(a2 + b2)

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

Example:

Suppose a=2 and b=2, Then

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

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

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

Approach:

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

a= 4
b= 2
(a+b)2+(a-b)2 = 40

Method-2: Java Program to Compute (a+b)2+(a-b)2 where Value of a and b are Given By Using User Defined Method and Pow() Function

Approach:

  • Declare two integer variables say a and b.
  • Prompt the user to enter the values to the corresponding variables by using Scanner class.
  • Then By using the formula compute (a+b)2+(a-b)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();
        //declare another double variable and assigned the formulated value to it.
        double c=2*(Math.pow(a,2)+ Math.pow(b,2));
        //Typecasting the value of c to integer value
        System.out.println("(a+b)2+(a-b)2= "+(int)c);	
    }
}
Output:

a= 4
b= 5
(a+b)2+(a-b)2= 82

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

Approach:

  • Declare two integer variables say a and b.
  • Prompt the user to enter the values to the corresponding variables by using Scanner class.
  • Then call a user defined method say computeValue() and pass a and b as parameter.
  • Then inside method by using the formula compute (a+b)2+(a-b)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();
        //call the function
        computeValue(a,b);
    }
    
    //define the method
    public static void computeValue(int a, int b)
    {
        //declare another integer variable and assigned the formulated value to it.
        int c=  2*((a*a) + (b*b));
        System.out.println("(a+b)2+(a-b)2 = "+c);
    }
}
Output:

a= 10
b= 20
(a+b)2+(a-b)2 = 1000

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.

Recommended Reading On: Java Program to Compute a^(1/x) where Value of a and x is Given

Try these: 

  1. Write A Program To Calculate (A+B)2 In Java
  2. Write A Program To Accept A And B And Calculate And Print (A+B)2
  3. Java Program For (A+B)^2
  4. Java A+B Program

Related Java Programs: