
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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 Articles
- Java Program to calculate the power using recursion
- Golang 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#?
- C++ program to Calculate Factorial of a Number Using Recursion
- Write a C# program to calculate a factorial using recursion
- Java program to calculate the GCD of a given number using recursion
- Raise x to the power n using Recursion in Java
- How to calculate fractional power using C#?
- C++ Program to Calculate Power of a Number
- Find power of a number using recursion in C#
- How to calculate power of three using C#?
- How to Find the Power of a Number Using Recursion in Python?
- Java program to calculate the power of a number
- C program to calculate power of a given number

Advertisements