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

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

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

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

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

(x+a)(x+b)=  x2 + x(a+b) + ab

Now we will convert this into a valid Java expression.

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

=  x2 + x(a+b) + ab

=(x*x)+x*(a+b)+(a*b)

Example:

Suppose x=4, a=2 and b=2, Then

(x+a)(x+b)= (x*x)+x*(a+b)+(a*b)
=(4*4)+4*(2+2)+(2*2)
=16

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

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

Approach:

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

Program:

class Main
{
public static void main(String[] args)
{
//declare the first integer variable with a integer value
int x= 4;
System.out.println("a= " +x);
//declare the second integer variable with a integer value
int a= 4;
System.out.println("a= " +a);
//declare the third 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=  (x*x)+x*(a+b)+(a*b);
System.out.println("(x+a)(x+b) = "+c);
}
}

Output:

a= 4
a= 4
b= 2
(x+a)(x+b) = 48

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

Approach:

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

Output:

x= 2
a= 3
b= 4
(x+a)(x+b)= 30

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

Approach:

• Declare three integer variables say x, 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 x, a and b as parameter.
• Then inside method by using the formula compute (x+a)(x+b)
• 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("x= ");
//Take the first input from the user.
int x= sc.nextInt();
System.out.print("a= ");
//Take the second input from the user.
int a= sc.nextInt();
System.out.print("b= ");
//Take the third input from the user.
int b= sc.nextInt();
//call the function
computeValue(x,a,b);
}

//define the method
public static void computeValue(int x,int a, int b)
{
//declare another integer variable and assigned the formulated value to it.
int c=  (x*x)+x*(a+b)+(a*b);
System.out.println("(x+a)(x+b) = "+c);
}
}

Output:

x= 10
a= 5
b= 6
(x+a)(x+b) = 240

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Programs: