### Select Articles

Finding power of a number in C, C++, Java and python

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() function

C

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

}

C++

#include

#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>

return 0;

}

JAVA

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

}

}

PYTHON 3

base = int(input(“Enter the base value : “))

exponent = int(input(“Enter the exponent value : “))

print(“Resultant value :”,int(pow(base, exponent)))

Output

Input- Enter the base value :3 Enter the exponent value :6 Output- Resultant value : 729

Program to find the power of a number using while loop

C

// 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++

/ 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>

return 0;

}

JAVA

// 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 3

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

Output

Input- Enter the base value :3 Enter the exponent value :6 Output- Resultant value : 729

Program to find the power of a number using recursion

C

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

#include

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++

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

#include

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>

return 0;

}

JAVA

// 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 3

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

Output

Input- Enter the base value :3 Enter the exponent value :6 Output- Resultant value : 729