Java Program to Compute a4+a2+1 where Value of a is Given

In the previous article we have discussed Java Program to Compute a4+a2b2+b4 where Value of a and b are Given

In this program we are going to see how to compute a4+a2+1 where value of a is given by using Java programming language.

Java Program to Compute a4+a2+1 where Value of a is Given

The formula of a4+a2+1 is given below.

a4+a2+1 =  (a2 – a + 1)(a2 + a + 1)

Now we will convert this into a valid Java expression.

Let x= a4+a2+1

= (a2 – a + 1)(a2 + a + 1)

= ((a*a) – a + 1)*( (a*a) + a + 1)

Example:

Suppose a=2 and b=2, Then

a4+a2+1 
= ((a*a) - a + 1)*( (a*a) + a + 1)
= ((2*2) - 2 + 1)*( (2*2) + 2 + 1)
= 21

Now let’s see different ways to compute a4+a2+1

Method-1: Java Program to Compute a4+a2+1 where Value of a is Given By Using Static Input Value

Approach:

  • Declare and initialize an integer variables say a.
  • By using the formula compute a4+a2+1
  • Print the result.

Program:

class Main
{
    public static void main(String[] args)
    {
        //declare the first integer variable with a integer value
        int a= 4;
        System.out.println("a= " +a);
        //declare another integer variable and assigned the formulated value to it.
        int c= ((a*a) - a + 1)*( (a*a) + a + 1);
        System.out.println("a4+a2+1= "+c);
    }
}
Output:

a= 4
a4+a2+1= 273

Method-2: Java Program to Compute a4+a2+1 where Value of a is Given By Using User Input Value and Pow() Function

Approach:

  • Declare an integer variables say a.
  • Prompt the user to enter the values to the corresponding variables by using Scanner class.
  • Then By using the formula compute a4+a2+1
  • 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();
        //declare another integer variable and assigned the formulated value to it.
        double c=( Math.pow(a,2)-a+1)*( Math.pow(a,2)+a+1);
        //Typecasting the value of c to integer value
        System.out.println("a4+a2+1= "+(int)c);	
    }
}
Output:

a= 20
a4+a2+1= 160401

Method-3: Java Program to Compute a4+a2+1 where Value of a is Given By Using User Defined Method

Approach:

  • Declare an integer variables say a .
  • 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 as parameter.
  • Then inside method by using the formula compute a4+a2+1
  • 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();
        //call the function
        computeValue(a);
    }
    
    //define the method
    public static void computeValue(int a)
    {
        //declare another integer variable and assigned the formulated value to it.
        int c= ((a*a) - a + 1)*( (a*a) + a + 1);
        System.out.println("a4+a2+1= "+c);
    }
}
Output:

a= 10
a4+a2+1= 10101

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: