In this tutorial you will learn about the C++ Program to Calculate Power Using Recursion and its application with practical example.
C++ program to Calculate Power Using Recursion
In this tutorial, we will learn to create a C++ program that will Calculate Power Using Recursion using C++ programming.
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Operators in C++ Programming.
- Basic Input and Output function in C++ Programming.
- Basic C++ programming.
- Conditional Statements in C++ programming.
- Arithmetic operations in C++ Programming.
What is Power?
The power of any number is shown as an exponent value of any number. This value shows exactly how many times the number is going to multiply by itself.
Algorithm:-
1 2 3 4 5 6 7 8 9 10 11 |
1. Declaring the variables for the program. 2. Taking the input number from the user. 3. Taking the power number from the user. 4. Calculating the power of that number. 5. Printing the result numbers. 6. End Program. |
Program to Calculate Power:-
In this tutorial, we will create a program that will take the base number and exponential number in input from the user and will calculate the answer using the recursion program.
The recursion is a program that repeats its execution again and again till a condition is satisfied.
Below is the program to Calculate Power Using Recursion.
Program :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <iostream> using namespace std; int calculatePower(int, int); int main() { //declaring the variable for the program. int bse, poR, rslt; //bse = it will hold the bse number. //poR = it will hold the bse number. //rslt = it will hold the bse number. //Taking the input from the user base number cout << "Enter bse number: "; cin >> bse; //Taking the input from the user exponential number cout << "Enter power number(positive integer): "; cin >> poR; rslt = calculatePower(bse, poR); cout << bse << "^" << poR << " = " << rslt; return 0; } int calculatePower(int bse, int poR) { if (poR != 0) return (bse*calculatePower(bse, poR-1)); else return 1; } |
Output:-
In the above program, we have first initialized the required variable.
- bse = it will hold the integer value.
- poR = it will hold the integer value.
- rslt = it will hold the integer value.
- j = it will hold the integer value.
- k = it will hold the integer value.
Program Logic Code to print.