In the previous article we have discussed about 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 am * an where value of a, m, and n are given by using Java programming language.
Java Program to Compute am * an where Value of a, m, and n are Given
The formula of am * an is given below.
am * an = am+n
Now we will convert this into a valid Java expression.
Let x= am * an
= am+n
Example:
Suppose a=2, m=2, and n=2, Then a^m * a^n = (a)^m+n = (2)^2+2 = 2^4 = 16
Now let’s see different ways to compute am * an
- 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*a^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 * an.
- 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 * an = "+res); } }
Output: a= 4 m= 4 n= 2 am * an = 4096
Method-2: Java Program to Compute a^m*a^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 * an.
- 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 * an = "+res); } }
Output: a= 4 m= 5 n= 6 am * an = 4194304
Method-3: Java Program to Compute a^m*a^n where Value of a, m and n are Given By Using User Defined Method 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 call a user defined method say
computeValue()
and pass a, m and n as parameter. - Then inside method by using the formula compute am * an.
- 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 * an = "+res); } }
Output: a= 3 m= 4 n= 5 am * an = 19683
Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.
Related Java Programs: