In the previous article we have discussed Java Program to Compute a3+b3 where Value of a and b are Given
In this program we are going to see how to compute a3-b3 where value of a and b are given by using Java programming language.
Java Program to Compute a3-b3 where Value of a and b are Given
The formula of a3-b3 is given below.
a3-b3 = (a – b)3 + 3ab(a – b) = a3 – b3 – 3a2b + 3ab2+ 3a2b – 3ab2
Now we will convert this into a valid Java expression.
Let x= a3-b3
= (a – b)3 + 3ab(a – b)
= a3 – b3 – 3a2b + 3ab2+ 3a2b – 3ab2
=(a*a*a)-(b*b*b)-(3*a*a*b)+(3*a*b*b)+( 3*a*a*b)-(3*a*b*b)
Example:
Suppose a=4 and b=2 Then a3+b3 =(a*a*a)-(b*b*b)-(3*a*a*b)+(3*a*b*b)+( 3*a*a*b)-(3*a*b*b) =(4*4*4)-(2*2*2)-(3*4*4*2)+(3*4*2*2)+ (3*4*4*2)-(3*4*2*2) =56
Now let’s see different ways to compute a3-b3
Method-1: Java Program to Compute a3-b3 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 a3-b3 .
- 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*a)-(b*b*b)-(3*a*a*b)+(3*a*b*b)+( 3*a*a*b)-(3*a*b*b); System.out.println("a3 - b3= "+c); } }
Output: a= 4 b= 2 a3 - b3= 56
Method-2: Java Program to Compute a3-b3 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 a3-b3 .
- 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)-(b*b*b)-(3*a*a*b)+(3*a*b*b)+( 3*a*a*b)-(3*a*b*b); System.out.println("a3 - b3= "+c); } }
Output: a= 4 b= 5 a3 - b3= -61
Method-3: Java Program to Compute a3-b3 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 a3-b3 .
- 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)-(b*b*b)-(3*a*a*b)+(3*a*b*b)+( 3*a*a*b)-(3*a*b*b); System.out.println("a3 - b3= "+c); } }
Output: a= 8 b= 10 a3 - b3= -488
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples
Related Java Programs: