In the previous article we have discussed about Java Program to Compute (a/b)x where Value of a, b and x are Given
In this program we are going to see how to compute a(1/x) where value of a and x is given by using Java programming language.
Java Program to Compute a^(1/x) where Value of a and x is Given
The formula of a(1/x) is given below.
a(1/x) = x√a
Now we will convert this into a valid Java expression.
Let x= a(1/x)
Example:
Suppose a=27 and x=3, Then a^(1/x) = 27^(1/3) = 3
Now let’s see different ways to compute a(1/x).
- By Using Static Input Value and Pow() Function
- By Using User Input Value and Pow() Function
- By Using User Defined Method
Method-1: Java Program to Compute a^(1/x) where Value of a and x is Given By Using Static Input Value and Pow() Function
Approach:
- Declare and initialize two double variables say
a
andx
. - By using the formula compute a(1/x).
- Print the result.
Program:
import java.lang.Math; class Main { public static void main(String[] args) { //declare the first double variable with a value double a= 125; System.out.println("a= " +a); //declare the second double variable with a value double x= 5; System.out.println("x= " +x); //declare another double variable and assigned the formulated value to it. double res= Math.pow(a,1/x); System.out.println("a^(1/x)= "+Math.round(res)); } }
Output: a= 125.0 x= 5.0 a^(1/x)= 3
Method-2: Java Program to Compute a^(1/x) where Value of a and x is Given By Using User Input Value and Pow() Function
Approach:
- Declare two double variables say
a
andx
. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then By using the formula compute a(1/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= "); //Declare an double variable and prompt the user to enter corresponding value to it. double a= sc.nextDouble(); System.out.print("x= "); //Declare another double variable and prompt the user to enter corresponding value to it. double x= sc.nextDouble (); //declare another double variable and assigned the formulated value to it. double res= Math.pow(a,1/x); System.out.println("a^(1/x)= "+Math.round(res)); } }
Output: a= 27 x= 3 a^(1/x)= 3
Method-3: Java Program to Compute a^(1/x) where Value of a and x is Given By Using User Defined Method
Approach:
- Declare two double variables say
a
andx
. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then call a user defined method say
computeValue()
and passa
andx
as parameter. - Then inside method by using the formula compute a(1/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= "); //Declare an double variable and prompt the user to enter corresponding value to it. double a= sc.nextDouble(); System.out.print("x= "); //Declare another double variable and prompt the user to enter corresponding value to it. double x= sc.nextDouble (); //call the function computeValue(a,x); } //define the method public static void computeValue(double a, double x) { //declare another double variable and assigned the formulated value to it. double res= Math.pow(a,1/x); System.out.println("a^(1/x)= "+Math.round(res)); } }
Output: a= 1728 x= 3 a^(1/x)= 12
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Related Java Programs: