Java program to calculate exponent – In this article, we will brief in on all the ways to calculate the exponent in Java programming.
Suitable examples and sample programs have been added to the article for better understanding. The compiler has also been added so that you can execute the programs easily.
The ways used in this article are as follows:
The exponent of a number is the number of times that particular number is multiplied with itself.
As you can see in the image uploaded above, an exponent is placed at the superscript position of a number which is known as the base.
In the example, 83 is given where 8 is the base and 3 is the exponent.
So, the number 8 is to be multiplied thrice.
Hence, the answer is:
83 = 8 * 8 * 8 = 512
Thus, the multiple ways to calculate the exponent in Java programming are as follows:
1.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class Exponent { public static void main(String args[]) { long x=7; long n=2; long r=x; for (int i=1;i<n;i++) { r=r*x; } System.out.println("exponent value of "+x+"^"+n+" is :"+r); } } |
Output:
1 |
exponent value of 7^2 is :49 |
2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import java.util.Scanner; class Exponent { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.println("Enter a base number: "); long x=sc.nextLong(); System.out.println("Enter a power number: "); long n=sc.nextLong(); long r=x; for (int i=1;i<n;i++) { r=r*x; } System.out.println("exponent value of "+x+"^"+n+" is :"+r); } } |
Output:
3.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class Exponent { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a base number: "); long x=sc.nextLong(); System.out.print("Enter a power number: "); long n=sc.nextLong(); long e=exponentCal(x,n); System.out.print("exponent value of "+x+"^"+n+" is :"+e); } static long exponentCal(long x,long n) { long r=x; for (int i=1;i<n;i++) { r=r*x; } return r; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
output:1 Enter a base number: 2 Enter a power number: 7 exponent value of 2^7 is :128 output:2 Enter a base number: 3 Enter a power number: 7 exponent value of 3^7 is :2187 output:3 Enter a base number: 5 Enter a power number: 5 exponent value of 5^5 is :3125 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
import java.util.Scanner; class ExponentCalculation { long r; ExponentCalculation(long x,long n) { r=x; for (int i=1;i<n;i++) { r=r*x; } } } class Exponent { public static void main(String args[]) { Scanner sc = new Scanner(System.in); System.out.print("Enter a base number: "); long x=sc.nextLong(); System.out.print("Enter a power number: "); long n=sc.nextLong(); ExponentCalculation e=new ExponentCalculation(x,n); System.out.print("exponent value of "+x+"^"+n+" is :"+e.r); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
output:1 Enter a base number: 10 Enter a power number: 5 exponent value of 10^5 is :100000 output:2 Enter a base number: 10 Enter a power number: 18 exponent value of 10^18 is :1000000000000000000 output:3 Enter a base number: 10 Enter a power number: 19 exponent value of 10^19 is :-8446744073709551616 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
class Exponent { public static void main(String args[]) { long x=Long.parseLong(args[0]); long n=Long.parseLong(args[1]); System.out.println(" base number is: "+x); System.out.println("power number is : "+n); long r=x; for (int i=1;i<n;i++) { r=r*x; } System.out.println("exponent value of "+x+"^"+n+" is :"+r); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
output:1 >javac Exponent.java >java Exponent 9 2 base number is: 9 power number is : 2 exponent value of 9^2 is :81 output:2 >java Exponent 5 4 base number is: 5 power number is : 4 exponent value of 5^4 is :625 output:3 >java Exponent 9 9 base number is: 9 power number is : 9 exponent value of 9^9 is :387420489 */ |