# Find the power of a number in C, C++, Java and python | faceprep

Program to find the power of a number is discussed here. Power of the number is the base raised to its exponent value. Power of a number can be found using loops, using recursion and also using library functions.

## Program to find power of a number using pow() fucntion

#include <stdio.h>
#include <math.h>

int main()
{
int base, exponent;
printf(“\nEnter the base value : “);
scanf(“%d”, &base);
printf(“\nEnter the exponent value : “);
scanf(“%d”, &exponent);
int result = pow(base,exponent);
printf(“\nResultant value : %d\n”, result);
return 0;
}

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
int base, exponent;
cout << “\nEnter the base value : “;
cin >> base;
cout << “\nEnter the exponent value : “;
cin >> exponent;
int result = pow(base,exponent);
cout << “\nResultant value : ” << result<< endl;
return 0;
}

// Java program to find the power of a number using recursion
import java.util.*;
public class Main
{
public static void main (String[] args)
{
int base,exponent;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the base value : “);
base = sc.nextInt();
System.out.print(“\nEnter the exponent value : “);
exponent = sc.nextInt();
int result = (int)Math.pow(base, exponent);
System.out.println(“\nResultant value : ” + result);

}
}

base = int(input(“Enter the base value : “))
exponent = int(input(“Enter the exponent value : “))
print(“Resultant value :”,int(pow(base, exponent)))

## Program to find the power of a number using while loop

// C program to find power of a number using loops

#include
int main()
{
int base, exponent;

long int result = 1;

printf(“\nEnter the base value : “);
scanf(“%d”, &base);

printf(“\nEnter the exponent value : “);
scanf(“%d”, &exponent);

while(exponent != 0)
{
result = result * base;
–exponent;
}

printf(“\nResultant value : %d\n”, result);

return 0;
}

// C++ program to find the power of a number using loops

#include
using namespace std;
int main()
{
int base, exponent;

long int result = 1;

cout << “\nEnter the base value : “;
cin >> base;

cout << “\nEnter the exponent value : “;
cin >> exponent;

while(exponent != 0)
{
result = result * base;
–exponent;
}

cout << “\nResultant value : ” << result << endl;

return 0;
}

// Java program to find the power of a number using loops

import java.util.*;
public class Main
{
public static void main (String[] args)
{
int base,exponent;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the base value : “);
base = sc.nextInt();
System.out.print(“\nEnter the exponent value : “);
exponent = sc.nextInt();
int result = 1;
while(exponent != 0)
{
result = result * base;
–exponent;
}
System.out.println(“\nResultant value : ” + result );

}
}

# Python program to find the power of a number

base = int(input(“Enter the base value : “))
exponent = int(input(“Enter the exponent value : “))
result = 1
while(exponent != 0):
result = result * base
exponent = exponent – 1
print(“Resultant value :”,result)

## Program to find the power of a number using recursion

// C program to find power of a number using recursion
#include <stdio.h>

int power_of_a_number(int base, int exponent)
{
if (exponent != 0)
return (base * power_of_a_number(base, exponent-1));
else
return 1;
}

int main()
{
int base, exponent;
printf(“\nEnter the base value : “);
scanf(“%d”, &base);
printf(“\nEnter the exponent value : “);
scanf(“%d”, &exponent);
printf(“\nResultant value : %d\n”, power_of_a_number(base,exponent));
return 0;
}

// C++ program to find the power of a number using recursion
#include <iostream>
using namespace std;

int power_of_a_number(int base, int exponent)
{
if (exponent != 0)
return (base * power_of_a_number(base, exponent-1));
else
return 1;
}

int main()
{
int base, exponent;
cout << “\nEnter the base value : “;
cin >> base;
cout << “\nEnter the exponent value : “;
cin >> exponent;

cout << “\nResultant value : ” << power_of_a_number(base, exponent) << endl;
return 0;
}

// Java program to find the power of a number using recursion
import java.util.*;
public class Main
{
static int power_of_a_number(int base, int exponent)
{
if (exponent != 0)
return (base * power_of_a_number(base, exponent-1));
else
return 1;
}

public static void main (String[] args)
{
int base,exponent;
Scanner sc = new Scanner(System.in);
System.out.print(“\nEnter the base value : “);
base = sc.nextInt();
System.out.print(“\nEnter the exponent value : “);
exponent = sc.nextInt();

System.out.println(“\nResultant value : ” + power_of_a_number(base, exponent));

}
}

# Python program to find power of a number using recursion

def power_of_a_number(base, exponent):
if (exponent != 0):
return (base * power_of_a_number(base, exponent-1))
else:
return 1

base = int(input(“Enter the base value : “))
exponent = int(input(“Enter the exponent value : “))
result = 1
print(“Resultant value :”,power_of_a_number(base, exponent))