Basic Java programming exercises

 Java Tutorial Java Exercises Data Structures

Java program to find power of a number using pow function

Write a Java program to input two numbers from user and find their power using pow() function. How to find power of a number in Java programming. How to use pow() function in Java programming.

Required knowledge

Arithmetic operators, Data types, Basic input/output

Java program to find power of any number (Using Standard Method)
```/**
* Java program to find power of any number (Using Standard Method)
*/

import java.util.Scanner;

class Test
{
public static void main(String arg[])
{
int n,p,result=1;
Scanner op=new Scanner(System.in);
System.out.print("Enter base: ");
n=op.nextInt();
System.out.print("Enter exponent: ");
p=op.nextInt();

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);

}
}

```

Output:

```Enter base: 3
Enter exponent: 2
3^2=9

```

Java program to find power of any number (Using For Loop)

```/**
* Java program to find power of any number (Using For Loop)
*/

import java.util.Scanner;

class Test
{
public static void main(String arg[])
{
int n,p,r=1;
Scanner op=new Scanner(System.in);
System.out.print("Enter base: ");
n=op.nextInt();
System.out.print("Enter exponent: ");
p=op.nextInt();

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);

}

}

```

Output:

```Enter base: 10
Enter exponent: 3
10^3=1000
```

Java program to find power of any number (Using Command Line Arguments)

```/**
* Java program to find power of any number (Using Command Line Arguments)
*/

import java.util.Scanner;

class Test
{
public static void main(String arg[])
{
int n,p,r=1;
Scanner op=new Scanner(System.in);
System.out.print("Enter base: ");
n=op.nextInt();
System.out.print("Enter exponent: ");
p=op.nextInt();

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);
}
}

```

Output:

```Enter base: 5
Enter exponent: 3
5^3=125
```

Java program to find power of any number (Using Function)

```/**
* Java program to find power of any number (Using Function)
*/

import java.util.Scanner;

class Test
{
public static void main(String arg[])
{
long n,p,r=1;
Scanner op=new Scanner(System.in);
System.out.print("Enter base: ");
n=op.nextLong();
System.out.print("Enter exponent: ");
p=op.nextLong();

r=Test.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;

}

}

```

Output:

```Enter base: 7
Enter exponent: 3
7^3=343

```

Java program to find power of any number (Using Class)

```/**
* Java program to find power of any number (Using Class)
*/

import 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 Test
{
public static void main(String arg[])
{
long n,p,r=1;
Scanner op=new Scanner(System.in);
System.out.print("Enter base: ");
n=op.nextLong();
System.out.print("Enter exponent: ");
p=op.nextLong();

Pow k=new Pow(n,p);

System.out.println(n+"^"+p+"="+k.r1);

}
}

```

Output:

```Enter base: 5
Enter exponent: 5
5^5=3125
```

Java program to find power of any number (Using Recursion)

```/**
* Java program to find power of any number (Using Recursion)
*/

import java.util.Scanner;

class Test
{
static long r=1;
public static void main(String arg[])
{
long n,p;
Scanner op=new Scanner(System.in);
System.out.print("Enter base: ");
n=op.nextLong();
System.out.print("Enter exponent: ");
p=op.nextLong();
Test.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;

Test.pow(n,p-1);
}

}
```

Output:

```Enter base: 2
Enter exponent: 5
2^5=32

```