- 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

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.

The program to find the power of a number using a non-recursive program is given as follows −

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

The program to find the power of a number using a recursive program is given as follows.

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

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

- Related Questions & Answers
- C program to calculate power of a given number
- Java program to calculate the power of a number
- How to calculate Power of a number using recursion in C#?
- Java program to calculate the power of a Given number using recursion
- C++ Program to Calculate Power Using Recursion
- How to calculate power of three using C#?
- C++ program to Calculate Factorial of a Number Using Recursion
- How to calculate fractional power using C#?
- 8085 program to find nth power of a number
- C++ Program to find whether a number is the power of two?
- C++ Program to calculate the number of odd days in given number of years
- How to calculate the power exponent value using C#?
- Golang Program to check the power of 4 of a given number
- Check if a number is a power of another number in C++
- Find power of a number using recursion in C#

Advertisements