Java Program to Compute (ab)x where Value of a, b and x are Given

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

In this program we are going to see how to compute (ab)x  where value of a, b and x are given.

Java Program to Compute (ab)x  where Value of a, b, and x are Given

The formula of (ab)x  is given below.

(ab)x  = ax * bx

 Now we will convert this into a valid Java expression.

Let x

= (ab)x

=  ax * bx

Example:

Suppose a=2, m=2, and n=2

Then,
(ab)^x 
= a^x * b^x
=2^2 * 2^2
=4 * 4
=16

Now let’s see different ways to compute (ab)x.

Method-1: Java Program to Compute (ab)x where Value of a, b and x are Given By Using pow() Function and Static Input Value

Approach:

  • Declare and initialize three integer variables say a, b and x.
  • By using the formula compute (ab)x .
  • Print the result.

Program:

import java.lang.Math;
public 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 the second integer variable with a integer value
        int b= 4;
        System.out.println("b= "+b);
        //declare the third integer variable with a integer value
        int x= 2;
        System.out.println("x= "+x);
        //declare another integer variable and assigned the formulated value to it.
        int res= (int)(Math.pow(a,x) * Math.pow(b,x));
        System.out.println("(ab)^x  = "+res);
    }
}
Output:

a= 4
b= 4
x= 2
(ab)^x  = 256

Method-2: Java Program to Compute (ab)x where Value of a, b and x are Given By Using pow() Function and User Input Value

Approach:

  • Declare three integer variables say a, b and x.
  • Prompt the user to enter the values to the corresponding variables by using Scanner class.
  • Then By using the formula compute (ab)x .
  • 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("x= ");
        //Take the third input from the user.
        int x= sc.nextInt();
        //declare another integer variable and assigned the formulated value to it.
        //declare another integer variable and assigned the formulated value to it.
        int res= (int)(Math.pow(a,x) * Math.pow(b,x));
        System.out.println("(ab)^x  = "+res);
    }
}

Output:

a= 2
b= 4
x= 5
(ab)^x  = 32768

Method-3: Java Program to Compute (ab)x where Value of a, b and x are Given By Using User Defined Method

Approach:

  • Declare three integer variables say a, b and x.
  • 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 x as parameter.
  • Then inside method by using the formula compute (ab)x .
  • 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("x= ");
        //Take the third input from the user.
        int x= sc.nextInt();
        //call the funtion
        computeValue(a,b,x);
    }
    
    //computeValue() method
    public static void computeValue(int a, int b,int x){
        //declare another integer variable and assigned the formulated value to it.
        int res= (int)(Math.pow(a,x) * Math.pow(b,x));
        System.out.println("(ab)^x  = "+res);
    }

}

Output:

a= 4
b= 3
x= 4
(ab)^x  = 20736

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Related Java Programs: