- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 of a Number
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
Power of a number can be calculated using recursive and non-recursive programs. Each of these are given as follows.
Power of a Number Using Non-Recursive Program
The program to find the power of a number using a non-recursive program is given as follows −
Example
#include<iostream> using namespace std; int power(int x, int y) { int i,power=1; if(y == 0) return 1; for(i=1;i<=y;i++) power=power*x; return power; } int main() { int x = 3; int y = 4; cout<<"x = "<<x<<endl;; cout<<"y = "<<y<<endl; cout<<"x^y = "<<power(x,y); return 0; }
x = 3 y = 4 x^y = 81
In the above program, the function power() is used to calculate the power of a number. It is a non-recursive function. In the function, a for loop is used which runs from 1 to y. For each iteration of the loop, x is multiplied with power.
So, x is multiplied with itself y times and the result is stored in power. This leads to x^y being stored in power. Then power is returned to the main() function.
The following code snippet demonstrates this −
int power(int x, int y) { int i, power = 1; if(y==0) return 1; for(i=1;i<=y;i++) power = power*x; return power; }
In main(), the values of x, y and x^y are displayed. This is shown in the code snippet given below −
cout<<"x = "<<x<<endl;; cout<<"y = "<<y<<endl; cout<<"x^y = "<<power(x,y);
Power of a Number Using Recursive Program
The program to find the power of a number using a recursive program is given as follows.
Example
#include<iostream> using namespace std; int power(int x, int y) { if (y == 0) return 1; else if (y%2 == 0) return power(x, y/2)*power(x, y/2); else return x*power(x, y/2)*power(x, y/2); } int main() { int x = 3; int y = 4; cout<<"x = "<<x<<endl;; cout<<"y = "<<y<<endl; cout<<"x^y = "<<power(x,y); return 0; }
Output
x = 3 y = 4 x^y = 81
In the above program, power() is a recursive function. If the value of y is 0, it returns 1. If y is even, it recursively calls itself with the values x and y/2 and returns power(x, y/2)*power(x, y/2). If y is odd, it recursively calls itself with the values x and y/2 and returns x*power(x, y/2)*power(x, y/2). This is demonstrated by the following code snippet.
int power(int x, int y) { if (y == 0) return 1; else if (y%2 == 0) return power(x, y/2)*power(x, y/2); else return x*power(x, y/2)*power(x, y/2); }
In main(), the values of x, y and x^y are displayed. This is shown in the code snippet given below.
cout<<"x = "<<x<<endl;; cout<<"y = "<<y<<endl; cout<<"x^y = "<<power(x,y);