In the previous article we have discussed Java Program to Compute a5+b5 where Value of a and b are Given
In this program we are going to see how to compute a5-b5 where value of a and b are given by using java programming language.
Java Program to Compute a5-b5 where Value of a and b are Given
The formula of a5-b5 is given below.
a5-b5 = (a – b)(a4 + a3b + a2b2 + ab3 + b4)
Now we will convert this into a valid Java expression.
Let x= a5-b5
= (a – b)(a4 + a3b + a2b2 + ab3 + b4)
= a5 + a4b + a3b2 + a2b3 + ab4 – a4b – a3b2 – a2b3 – ab4 – b5
= (a*a*a*a*a) + (a*a*a*a*b) + (a*a*a*b*b) + (a*a*b*b*b) + (a*b*b*b*b)- (a*a*a*a*b) – (a*a*a*b*b) – (a*a*b*b*b) – (a*b*b*b*b)- (b*b*b*b*b)
Example:
Suppose a=2 and b=2, then a5-b5 = (a*a*a*a*a) + (a*a*a*a*b) + (a*a*a*b*b) + (a*a*b*b*b) + (a*b*b*b*b)- (a*a*a*a*b) - (a*a*a*b*b) - (a*a*b*b*b) - (a*b*b*b*b)- (b*b*b*b*b) = (2*2*2*2*2)+ (2*2*2*2*2)+ (2*2*2*2*2)+ (2*2*2*2*2)+ (2*2*2*2*2)- (2*2*2*2*2)- (2*2*2*2*2)- (2*2*2*2*2)- (2*2*2*2*2)+ (2*2*2*2*2) = 0
Now let’s see different ways to compute a5-b5
Method-1: Java Program to Compute a5-b5 where Value of a and b are Given By Using Static Input Value
Approach:
- Declare and initialize two integer variables say
aandb. - By using the formula compute a5-b5 .
- 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*a*a) + (a*a*a*a*b) + (a*a*a*b*b) + (a*a*b*b*b) + (a*b*b*b*b)- (a*a*a*a*b) - (a*a*a*b*b) - (a*a*b*b*b) - (a*b*b*b*b)- (b*b*b*b*b);
System.out.println("a5 - b5= "+c);
}
}
Output: a= 4 b= 2 a5 - b5= 992
Method-2: Java Program to Compute a5-b5 where Value of a and b are Given By Using pow()
Approach:
- Declare two integer variables say
aandb. - Take the value of
aandbas user input by using Scanner class. - Then By using the formula compute a5-b5
- Here we have used inbuilt
pow()function to compute the result. - 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,5)) + (Math.pow(a,4)*b)+ (Math.pow(a,3)* Math.pow(b,2)) + (Math.pow(a,2)* Math.pow(b,3)) + (a* Math.pow(b,4))- (Math.pow(a,4)*b) - (Math.pow(a,3)* Math.pow(b,2)) - (Math.pow(a,2)* Math.pow(b,3)) - (a* Math.pow(b,4))- (Math.pow(b,5));
//Typecasting the value of c to integer value
System.out.println("a5 - b5= "+(int)c);
}
}
Output: a= 4 b= 5 a5 - b5= -2101
Method-3: Java Program to Compute a5-b5 where Value of a and b are Given By Using User Defined Method
Approach:
- Declare two integer variables say
aandb. - 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 a5-b5 .
- 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*a) + (a*a*a*a*b) + (a*a*a*b*b) + (a*a*b*b*b) + (a*b*b*b*b)- (a*a*a*a*b) - (a*a*a*b*b) - (a*a*b*b*b) - (a*b*b*b*b)- (b*b*b*b*b);
System.out.println("a5 - b5= "+c);
}
}
Output: a= 8 b= 10 a5 - b5= -67232
Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language.
Related Java Programs: