- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# C++ Program to Calculate Power Using Recursion

The power of a number can be calculated as x^y where x is the number and y is its power.

For example.

Let’s say, x = 2 and y = 10 x^y =1024 Here, x^y is 2^10

A program to find the power using recursion is as follows.

## Example

#include <iostream> using namespace std; int FindPower(int base, int power) { if (power == 0) return 1; else return (base * FindPower(base, power-1)); } int main() { int base = 3, power = 5; cout<<base<<" raised to the power "<<power<<" is "<<FindPower(base, power); return 0; }

## Output

3 raised to the power 5 is 243

In the above program, the function findPower() is a recursive function. If the power is zero, then the function returns 1 because any number raised to power 0 is 1. If the power is not 0, then the function recursively calls itself. This is demonstrated using the following code snippet.

int FindPower(int base, int power) { if (power == 0) return 1; else return (base * findPower(base, power-1)); }

In the main() function, the findPower() function is called initially and the power of a number is displayed.

This can be seen in the below code snippet.

3 raised to the power 5 is 243

- Related Questions & Answers
- Java Program to calculate the power using recursion
- Java program to calculate the power of a Given number using recursion
- How to calculate Power of a number using recursion in C#?
- Write a C# program to calculate a factorial using recursion
- C++ program to Calculate Factorial of a Number Using Recursion
- How to calculate fractional power using C#?
- Java program to calculate the GCD of a given number using recursion
- C++ Program to Calculate Power of a Number
- How to calculate power of three using C#?
- Raise x to the power n using Recursion in Java
- Java program to calculate the power of a number
- C program to calculate power of a given number
- How to calculate the power exponent value using C#?
- Find power of a number using recursion in C#
- Calculate the Power of a Number in Java Program

Advertisements