
- 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 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);
- Related Articles
- C program to calculate power of a given number
- Java program to calculate the power of a number
- Swift Program to Calculate the Power of a Number
- Haskell Program to Calculate the Power of a Number
- Kotlin Program to Calculate the Power of a Number
- Calculate the Power of a Number in Java Program
- 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 Power Using Recursion
- C++ program to Calculate Factorial of a Number Using Recursion
- C++ Program to find whether a number is the power of two?
- How to calculate power of three using C#?
- 8085 program to find nth power of a number
- Java Program to calculate the power using recursion
- Haskell Program to calculate the power using Recursion
