In the previous article we have discussed Java Program to Compute (a+b+c)(b+c-a)(c+a-b)(a+b-c) where Value of a, b and c are Given
In this program we are going to see how to compute (a+b+c)(bc+ca+ab)-abc where value of a, b, and c are given.
Java Program to Compute (a+b+c)(bc+ca+ab)-abc where Value of a, b and c are Given
The formula of (a+b+c)(bc+ca+ab)-abc is given below.
(a+b+c)(bc+ca+ab)-abc = (a+b)(b+c)(c+a)
Now we will convert this into a valid Java expression.
Let x= (a+b+c)(bc+ca+ab)-abc
=(a+b)(b+c)(c+a)
=((a+b)*(b+c)*(c+a))
Example:
Suppose a=4, b=2 and c= 3 ,Then (a+b+c)(bc+ca+ab)-abc = ((a+b)*(b+c)*(c+a)) =((4+2)*(2+3)*(3+4)) = 210
Now let’s see different ways to compute (a+b+c)(bc+ca+ab)-abc
Method-1: Java Program to Compute (a+b+c)(bc+ca+ab)-abc where Value of a, b and c are Given By Using Static Input Value
Approach:
- Declare and initialize three integer variables say
a,bandc - By using the formula compute (a+b+c)(bc+ca+ab)-abc .
- 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 the third integer variable with a integer value
int c= 3;
System.out.println("c= "+c);
//declare another integer variable and assigned the formulated value to it.
int d=((a+b)*(b+c)*(c+a));
System.out.println("(a+b+c)(bc+ca+ab)-abc= "+d);
}
}
Output: a= 4 b= 2 c= 3 (a+b+c)(bc+ca+ab)-abc= 210
Method-2: Java Program to Compute (a+b+c)(bc+ca+ab)-abc where Value of a, b and c are Given By Using User Input Value
Approach:
- Declare three integer variables say
a,bandc - Prompt the user to enter the values to the corresponding variables.
- Then By using the formula compute (a+b+c)(bc+ca+ab)-abc .
- 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 third input from the user.
int c= sc.nextInt();
//declare another integer variable and assigned the formulated value to it.
int d=((a+b)*(b+c)*(c+a));
System.out.println("(a+b+c)(bc+ca+ab)-abc= "+d);
}
}
Output: a= 12 b= 23 c= 34 (a+b+c)(bc+ca+ab)-abc= 91770
Method-3: Java Program to Compute (a+b+c)(bc+ca+ab)-abc where Value of a, b and c are Given By Using User Defined Method
Approach:
- Declare three integer variables say
a,bandc - Prompt the user to enter the values to the corresponding variables.
- Then call the user defined method say
computeValue()and passa,bandcas parameter. - Then inside that method by using the formula compute (a+b+c)(bc+ca+ab)-abc.
- 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 third 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=((a+b)*(b+c)*(c+a));
System.out.println("(a+b+c)(bc+ca+ab)-abc= "+d);
}
}
Output: a= 11 b= 22 c= 33 (a+b+c)(bc+ca+ab)-abc= 79860
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.
Related Java Programs: