Java Program To Calculate Power Of Number – In this article, we will detail in on the several ways to calculate the power of a number in Java programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The several methods used in this specific piece are as follows:
- Using Standard Method
- Using For Loop
- Using Command Line Arguments
- Using Function
- Using Class
- Using Recursion
As we all know, the power of a number is the times a number is multiplied with itself. The power of a number, in more simpler terms, is the number of times you have to multiply the number by itself.
For example, 5^3 is called as 5 to the power of 3.
Here, 5^3 = 5*5*5 = 125.
Similarly, 5^(-2) = (1/5)^2 = 1/25.
The power of 0 will always give the result as 1, no matter what the number is, unless if it is undefined or infinite.
As given in the photo above, the powers of the number 11 from 0 to 11 are mentioned there. It is clearly seen that 11 is multiplied by the result once after the other as the power increases.
Thus, the manifold ways to calculate the power of a number in Java programming are as follows:
Using Standard Method
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; { public static void main(String arg[]) { int n=5,p=3,result=1; if(n>=0&&p==0) { result=1; } else if(n==0&&p>=1) { result=0; } else { for(int i=1;i<=p;i++) { result=result*n; } } System.out.println(n+"^"+p+"="+result); } } |
1 |
5^3=125 |
Using For Loop
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 30 31 32 33 34 35 36 37 38 39 40 |
java.util.Scanner; class Power { public static void main(String arg[]) { long n,p,r=1; Scanner sc=new Scanner(System.in); System.out.println("enter number"); n=sc.nextLong(); System.out.println("enter power"); p=sc.nextLong(); if(n>=0&&p==0) { r =1; } else if(n==0&&p>=1) { r=0; } else { for(int i=1;i<=p;i++) { r=r *n; } } System.out.println(n+"^"+p+"="+r); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
1. enter number 6 enter power 6 6^6=46656 2. enter number 8 enter power 9 8^9=134217728 |
Using Command Line Arguments
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 30 |
java.util.Scanner; class Power { public static void main(String args[]) { long n,p,r=1; n=Long.parseLong(args[0]); p=Long.parseLong(args[1]); if(n>=0&&p==0) { r =1; } else if(n==0&&p>=1) { r=0; } else { for(int i=1;i<=p;i++) { r=r *n; } } System.out.println(n+"^"+p+"="+r); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
javac Power.java java Power 4 5 4^5=1024 javac Power.java java Power 0 0 0^0=1 javac Power.java java Power 0 1 0^1=0 javac Power.java java Power 100 20 100^20=-5047021154770878464 javac Power.java java Power 100 3 100^3=1000000 |
Using Function
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
java.util.Scanner; class Power { public static void main(String args[]) { long n,p,r=1; Scanner sc=new Scanner(System.in); System.out.println("enter number"); n=sc.nextLong(); System.out.println("enter power"); p=sc.nextLong(); r=Power.calpower(n,p); System.out.println(n+"^"+p+"="+r); } static long calpower(long n1,long p1) { long r1=1; if(n1>=0&&p1==0) r1 =1; else if(n1==0&&p1>=1) r1=0; else for(int i=1;i<=p1;i++) r1=r1 *n1; return r1; } } |
1 2 3 4 5 |
enter number 51 enter power 6 51^6=17596287801 |
Using Class
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 30 31 32 33 34 35 36 37 38 39 40 |
java.util.Scanner; class Pow { long r1=1; Pow(long n1,long p1) { if(n1>=0&&p1==0) r1 =1; else if(n1==0&&p1>=1) r1=0; else for(int i=1;i<=p1;i++) r1=r1 *n1; } } class Power { public static void main(String args[]) { long n,p; Scanner sc=new Scanner(System.in); System.out.println("enter number"); n=sc.nextLong(); System.out.println("enter power"); p=sc.nextLong(); Pow k=new Pow(n,p); System.out.println(n+"^"+p+"="+k.r1); } } |
1 2 3 4 5 |
enter number 6 enter power 5 6^5=7776 |
Using Recursion
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 30 31 32 33 34 35 36 37 38 39 40 41 42 |
java.util.Scanner; class Power { static long r=1; public static void main(String arg[]) { long n,p; Scanner sc=new Scanner(System.in); System.out.println("enter number"); n=sc.nextLong(); System.out.println("enter power"); p=sc.nextLong(); Power.pow(n,p); System.out.println(n+"^"+p+"="+r); } static void pow(long n,long p) { if(p<=0) { return; } else if(n==0 && p>=1) { r=0; return; } else r=r*n; Power.pow(n,p-1); } } |
1 2 3 4 5 |
enter number 5 enter power 5 5^5=3125 |