In the previous article we have discussed Java Program to Compute (a+b)4 where Value of a and b are Given
In this program we are going to see how to compute (a-b)4 where value of a and b are given by using Java programming language.
Java Program to Compute (a-b)4 where Value of a and b are Given
The formula of (a – b)4 is given below.
(a – b)4 = a4 + b4 – 4a3b + 6a2b2 – 4ab3
Now we will convert this into a valid Java expression.7.
Let x= (a-b)4
= a4 + b4 – 4a3b + 6a2b2 – 4ab3
= (a * a * a * a) + (b * b * b * b) – (4 * a * a * a * b) + (6 * a * a * b * b) – (4 * a * b * b * b)
Example:
Suppose a=2 and b=2, then (a-b)4 = (a * a * a * a) + (b * b * b * b) - (4 * a * a * a * b) + (6 * a * a * b * b) - (4 * a * b * b * b) = (2*2*2*2)+(2*2*2*2)-(4*2*2*2*2)+(6*2*2*2*2)-(4*2*2*2*2) = 0
Now let’s see different ways to compute (a-b)4
Method-1: Java Program to Compute (a-b)4 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 (a-b)4
- Print the result.
Program:
class Main { public static void main(String[] args) { //declare the first integer variable with an integer value int a= 15; System.out.println("a= " +a); //declare the second integer variable with an integer value int b= 7; System.out.println("b= "+b); //declare another integer variable and assigned the formulated value to it. int c=(a*a*a*a) + (b*b*b*b) - (4*a*a*a*b) + (6*a*a*b*b) - (4*a*b*b*b); System.out.println("(a-b)4= "+c); } }
Output: a= 15 b= 7 (a-b)4= 4096
Method-2: Java Program to Compute (a-b)4 where Value of a and b are Given By Using User Input Value
Approach:
- Declare two integer variables say
a
andb
. - Take the value of a and b as user input by using Scanner class.
- Then By using the formula compute (a-b)4
- 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(); //declare another integer variable and assigned the formulated value to it. int c=(a*a*a*a) + (b*b*b*b) - (4*a*a*a*b) + (6*a*a*b*b) - (4*a*b*b*b); System.out.println("(a-b)4= "+c); } }
Output: a= 20 b= 11 (a-b)4= 6561
Method-3: Java Program to Compute (a-b)4 where Value of a and b are Given By Using User Defined Method
Approach:
- Declare two integer variables say
a
andb
. - 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 (a-b)4
- 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*a*a) + (b*b*b*b) - (4*a*a*a*b) + (6*a*a*b*b) - (4*a*b*b*b); System.out.println("(a-b)4= "+c); } }
Output: a= 8 b= 10 (a-b)4= 16
Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.
Related Java Programs: