In the previous article we have discussed Java Program to Compute a3-b3+c3+3abc where Value of a, b and c are Given
In this program we are going to see how to compute a4+a2b2+b4 where value of a and b are given by using Java programming language.
Java Program to Compute a4+a2b2+b4 where Value of a and b are Given
The formula of a4+a2b2+b4 is given below
a4+a2b2+b4 = (a2 + b2 + ab)(a2 + b2 – ab)
Now we will convert this into a valid Java expression
Let x= a4+a2b2+b4
= (a2 + b2 + ab)(a2 + b2 – ab)
= ((a*a) + (b*b) – (a*b))*((a*a) + (b*b) – (a*b))
Example:
Suppose a=2 and b=2, Then a2+a2b2+b4 = ((a*a) + (b*b) – (a*b))*((a*a) + (b*b) – (a*b)) = ((2*2) + (2*2) – (2*2))*((2*2) + (2*2) – (2*2))
Now let’s see different ways to compute a4+a2b2+b4
- By Using Static Input Value
- By Using User Input Value and Pow() Function
- By Using User Defined Method
Method-1: Java Program to Compute a4+a2b2+b4 where Value of a and b are Given By Using Static Input Value
Approach:
- Declare and initialize two integer variables say
a
andb
. - By using the formula compute a4+a2b2+b4
- 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*a) + (b*b) + (a*b))*((a*a) + (b*b) - (a*b)); System.out.println("a4+a2b2+b4 = "+c); } }
Output: a= 4 b= 2 a4+a2b2+b4 = 336
Method-2: Java Program to Compute a4+a2b2+b4 where Value of a and b are Given By Using User Input Value and Pow() Function
Approach:
- Declare two integer variables say
a
andb
. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then By using the formula compute a4+a2b2+b4
- 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 integer variable and assigned the formulated value to it. double c=(Math.pow(a,2)+Math.pow(b,2)+(a*b))*( Math.pow(a,2)+Math.pow(b,2)-(a*b)); //Typecasting the value of c to integer value System.out.println("a4+a2b2+b4= "+(int)c); } }
Output: a= 12 b= 11 a4+a2b2+b4= 52801
Method-3: Java Program to Compute a4+a2b2+b4 where Value of a and b are Given By Using User Defined Method
Approach:
- Declare two integer variables say
a
andb
. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then call a user defined method say
computeValue()
and passa
andb
as parameter. - Then inside method by using the formula compute a4+a2b2+b4.
- 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= ((a*a) + (b*b) + (a*b))*((a*a) + (b*b) - (a*b)); System.out.println("a4+a2b2+b4 = "+c); } }
Output: a= 4 b= 5 a4+a2b2+b4 = 1281
If you are new to Java and want to learn the java coding skills too fast. Try practicing the core java programs with the help of the Java basic programs list available.
Related Java Programs: