In the previous article we have discussed about Java Program to Compute a^m/a^n where Value of a, m and n are Given
In this program we are going to see how to compute (am)n where value of a, m, and n are given by using Java programming language.
Java Program to Compute (am)n where Value of a, m, and n are Given
The formula of (am)n is given below.
(am)n = am*n
Now we will convert this into a valid Java expression.
Let x= (am)n
= am*n
Example:
Suppose a=2, m=2, and n=2, Then (a^m)^n = (a)^m*n =(2)^(2*2) = (2)^4 =16
Now let’s see different ways to compute (am)n
- 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^m)^n where Value of a, m, and n are Given By Using Static Input Value and Pow() Function
Approach:
- Declare and initialize three integer variables say
a
,m
, andn
. - By using the formula compute (am)n .
- Print the result.
Program:
import java.lang.Math; public 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 m= 4; System.out.println("m= "+m); //declare the third integer variable with an integer value int n= 2; System.out.println("n= "+n); //declare another integer variable and assigned the formulated value to it. int res= (int)Math.pow(a,m*n); System.out.println("(am)n = "+res); } }
Output: a= 4 m= 4 n= 2 (am)n = 65536
Method-2: Java Program to Compute (a^m)^n where Value of a, m, and n are Given By Using User Input and Pow() Function
Approach:
- Declare three integer variables say
a
,m
, andn
. - Prompt the user to enter the values to the corresponding variables by using Scanner class.
- Then By using the formula compute (am)n .
- 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("m= "); //Take the second input from the user. int m= sc.nextInt(); System.out.print("n= "); //Take the third input from the user. int n= sc.nextInt(); //declare another integer variable and assigned the formulated value to it. int res= (int)Math.pow(a,m*n); System.out.println("(am)n = "+res); } }
Output: a= 5 m= 2 n= 3 (am)n = 15625
Method-3: Java Program to Compute (a^m)^n where Value of a, m, and n are Given By Using User Defined Method
Approach:
- Declare three integer variables say
a
,m
, andn
. - 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
,m
andn
as parameter. - Then inside method by using the formula compute (am)n .
- 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("m= "); //Take the second input from the user. int m= sc.nextInt(); System.out.print("n= "); //Take the third input from the user. int n= sc.nextInt(); //call the funtion computeValue(a,m,n); } //define the method public static void computeValue(int a, int m,int n) { //declare another integer variable and assigned the formulated value to it. int res= (int)Math.pow(a,m*n); System.out.println("(am)n = "+res); } }
Output: a= 7 m= 3 n= 4 (am)n = 2147483647
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.
Related Java Programs: