In the previous article we have discussed Java Program to Compute (ab)x where Value of a, b and x are Given
In this program we are going to see how to compute (a/b)x where value of a, b, and x are given.
Java Program to Compute (a/b)x where Value of a, b, and x are Given
The formula of (a/b)x is given below.
(a/b)x = ax / bx
Now we will convert this into a valid Java expression.
Let x = (a/b)x
= ax / bx
Example:
Suppose a=2, m=2, and n=2 Then, (a/b)^x = a^x / b^x = 2^2 / 2^2 = 4 / 4 = 1
Now let’s see different ways to compute (a/b)x.
- By Using pow() Function and Static Input Value
- By Using pow() Function and User Input Value
- By Using User Defined Method
Method-1: Java Program to Compute (a/b)^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,bandx. - By using the formula compute (a/b)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 double variable and assigned the formulated value to it.
double res= (Math.pow(a,x) / Math.pow(b,x));
System.out.println("(a/b)^x = "+res);
}
}
Output: a= 4 b= 4 x= 2 (a/b)^x = 1.0
Method-2: Java Program to Compute (a/b)^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,bandx. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then By using the formula compute (a/b)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 double variable and assigned the formulated value to it.
double res= (Math.pow(a,x) / Math.pow(b,x));
System.out.println("(a/b)^x = "+res);
}
}
Output: a= 9 b= 6 x= 3 (a/b)^x = 3.375
Method-3: Java Program to Compute (a/b)^x where Value of a, b and x are Given By Using User Defined Method
Approach:
- Declare three integer variables say
a,bandx. - 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,bandxas parameter. - Then inside method by using the formula compute (a/b)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);
}
//define the method
public static void computeValue(int a, int b,int x)
{
//declare another double variable and assigned the formulated value to it.
double res= (Math.pow(a,x) / Math.pow(b,x));
System.out.println("(a/b)^x = "+res);
}
}
Output: a= 12 b= 5 x= 4 (a/b)^x = 33.1776
Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output.
Related Java Programs: