Java Program to Compute a3-b3+c3+3abc where Value of a, b, and c are Given

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 a3-b3+c3+3abc where value of a, b, and c are given by using Java programming language.

Java Program to Compute a3-b3+c3+3abc where Value of a, b, and c are Given

The formula of a3-b3+c3+3abc is given below.

a3-b3+c3+3abc = (a – b + c)(a2 + b2 + c2 + ab + bc – ca)

Now we will convert this into a valid Java expression.

Let x= a3-b3+c3+3abc

= (a -b+c)(a2 + b2 + c2 + ab + bc – ca)

= (a-b+c)*( (a * a) + (b * b) +(c*c)+ (a * b)+(b * c)-( c * a))

Example:

Suppose a=2, b=2, and c=2, Then

a3-b3+c3+3abc 
= (a-b+c)*( (a * a) + (b * b) +(c*c)+ (a * b)+(b * c)-( c * a))
= (2-2+2)*((2 * 2) + (2 * 2) +(2*2)+( 2 * 2)+( 2 * 2)-( 2 * 2))
= -32

Now let’s see different ways to compute a3-b3+c3+3abc

Method-1: Java Program to Compute a3-b3+c3+3abc where Value of a, b, and c are Given By Using Static Input Value

Approach:

  • Declare and initialize three integer variables say a, b and c.
  • By using the formula compute a3-b3+c3+3abc
  • 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= 4;
        System.out.println("b= "+b);
        //declare the third integer variable with an integer value
        int c= 2;
        System.out.println("c= "+c);
        //declare another integer variable and assigned the formulated value to it.
        int d=(a-b+c)*( (a * a) + (b * b) +(c*c)+ (a * b)+(b * c)-( c * a));
        System.out.println("a3-b3+c3+3abc = "+d);
    }
}
Output:

a= 4
b= 4
c= 2
a3-b3+c3+3abc = 104

Method-2: Java Program to Compute a3-b3+c3+3abc where Value of a, b, and c are Given By Using User Input Value and Pow() Function

Approach:

  • Declare three integer variables say a, b and c.
  • Prompt the user to enter the values to the corresponding variables by using Scanner class.
  • Then By using the formula compute a3-b3+c3+3abc
  • 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();
        System.out.print("c= ");
        //Take the third input from the user.
        int c= sc.nextInt();
        //declare another integer variable and assigned the formulated value to it.
        double  d=(a-b+c)*((Math.pow(a,2)) + (Math.pow(b,2)) +( Math.pow(c,2))+(a * b)+( b * c)-( c * a));
        System.out.println("a3-b3+c3+3abc = "+(int)d);
    }
}
Output:

a= 3
b= 4
c= 5
a3-b3+c3+3abc = 268

Method-3: Java Program to Compute a3-b3+c3+3abc where Value of a, b, and c are Given By Using User Defined Method

Approach:

  • Declare three integer variables say a, b and c.
  • Prompt the user to enter the values to the corresponding variables 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-b3+c3+3abc
  • 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 third input from the user.
        int c= sc.nextInt();
        //call the funtion
        computeValue(a,b,c);
    }
    
    //define the 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+c)*( (a * a) + (b * b) +(c*c)+ (a * b)+(b * c)-( c * a));
        System.out.println("a3-b3+c3+3abc = "+d);
    }
}
Output:

a= 10
b= 20
c= 30
a3-b3+c3+3abc = 38000

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: