In the previous article we have discussed Java Program to Compute a(b2-c2)+b(c2-a2)+c(a2-b2) where Value of a, b and c are Given
In this program we are going to see how to compute a3(b-c)+b3(c-a)+c3(a-b) where value of a, b, and c are given by using Java Programming language.
Java Program to Compute a3(b-c)+b3(c-a)+c3(a-b) where Value of a, b, and c are Given
The formula of a3(b-c)+b3(c-a)+c3(a-b) is given below.
a3(b-c)+b3(c-a)+c3(a-b) = (a-b)(b-c)(a-c)(a+b+c)
Now we will convert this into a valid Java expression.
Let x= a3(b-c)+b3(c-a)+c3(a-b)
=(a-b)(b-c)(a-c)(a+b+c)
= (a-b)*(b-c)* (a-c)*(a+b+c)
Example:
Suppose a=4, b=2 and c= 3, Then a3(b-c)+b3(c-a)+c3(a-b) = (a-b)*(b-c)* (a-c)*(a+b+c) = (4-2)*(2-3)*(4-3)*(4+2+3) = -18
Now let’s see different ways to compute a3(b-c)+b3(c-a)+c3(a-b)
Method-1: Java Program to Compute a3(b-c)+b3(c-a)+c3(a-b) where Value of a, b, and c are Given By Using Static Input Value
Approach:
- Declare and initialize three integer variables say
a,b, andc. - By using the formula compute a3(b-c)+b3(c-a)+c3(a-b)
- 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=(a-b)*(b-c)* (a-c)*(a+b+c);
System.out.println("a3(b-c)+b3(c-a)+c3(a-b)= "+d);
}
}
Output: a= 4 b= 2 c= 3 a3(b-c)+b3(c-a)+c3(a-b)= -18
Method-2: Java Program to Compute a3(b-c)+b3(c-a)+c3(a-b) where Value of a, b, and c are Given By Using User Input Value
Approach:
- Declare three integer variables say
a,b, andc. - Take the value of
a,b, andcas user input by using Scanner class. - Then By using the formula compute a3(b-c)+b3(c-a)+c3(a-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("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 integer variable and assigned the formulated value to it.
int d=(a-b)*(b-c)* (a-c)*(a+b+c);
System.out.println("a3(b-c)+b3(c-a)+c3(a-b)= "+d);
}
}
Output: a= 23 b= 45 c= 65 a3(b-c)+b3(c-a)+c3(a-b)= -2457840
Method-3: Java Program to Compute a3(b-c)+b3(c-a)+c3(a-b) where Value of a, b, and c are Given By Using User Defined Method
Approach:
- Declare three integer variables say
a,b, andc. - Take the value of
a,b, andcas 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 a3(b-c)+b3(c-a)+c3(a-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("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);
}
//computeValue() user defined 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)* (a-c)*(a+b+c);
System.out.println("a3(b-c)+b3(c-a)+c3(a-b)= "+d);
}
}
Output: a= 12 b= 23 c= 21 a3(b-c)+b3(c-a)+c3(a-b)= 11088
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: