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

In the previous article we have discussed Java Program to Compute an-bn when n is a natural number

In this program we are going to see how to compute  2(a2+b2)   where value of a and b are given by using Java Programming language. For example, Take two positive integers from the user verify if a b 3 a 3 b 3 3a 2b 3ab 2 in java,  a1 b2 c 3 program in java by using Equations in java and Java equation solver.

Also Read: Java Program to Compute an-bn when n is a Natural Number

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

The formula of 2(a2+b2) is given below.

2(a2+b2)  = (a + b)2 + (a – b)2 =[( a + b )( a + b )] + [( a – b )( a – b )]

Now we will convert this into a valid Java expression.

Let x= 2(a2+b2)

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

Example:

Suppose a=4 and b=2, then

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

Now let’s see different ways to compute 2(a2+b2)

Method-1: Java Program to Compute 2(a2+b2) 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 2(a2+b2).
  • 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 another integer variable and assigned the formulated value to it.
        int c=(( a + b ) * ( a + b )) + (( a - b )*( a - b ));
        System.out.println("2(a2+b2)= "+c);
    }
}
Output:

a= 4
b= 2
2(a2+b2)= 40

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

Approach:

  • Declare two integer variables say a and b.
  • Take the value of a and b as user input by using Scanner class.
  • Then By using the formula compute 2(a2+b2).
  • 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=(Math.pow(a+b,2)) + (Math.pow(a-b,2));
        System.out.println("2(a2+b2)= "+(int)c);
    }
}
Output:

a= 4
b= 5
2(a2+b2)= 82

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

Approach:

  • Declare two integer variables say a and b.
  • Take the value of a and b as user input 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 2(a2+b2).
  • 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 user defined method
        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=(( a + b )*( a + b )) + (( a - b )*( a - b ));
        System.out.println("2(a2+b2)= "+c);
    }
}
Output:

a= 8
b= 10
2(a2+b2)= 328

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

Answer yourself:

  • Write a program in java to find the roots of a quadratic equation?
  • Write a program that solves for x in a quadratic equation of the form?
  • a b whole square program in java?
  • Quadratic equation in java?
  • a=1 b=2 c 3 program in java?

Related Java Programs: