C++ program to the Power of a number
Power of a number in C++
In this post we will see various methods to calculate Power of a Number in C++.
We need the following to calculate power of number - Base : 5 Exponent : 3 Power = 53 = 125
Methods
- Method 1: Using Pow(base, exp) method in math.h library
- Method 2: Using Iterative Loop.
- Method 3: Enhanced iterative loop to handle negative exponent too.
- Method 4: Recursive approach
Method 1 (with inbuilt function)
This method handles all cases, when exponent/bases are integers/decimals or positive/negative
Code
// This method handles all cases, // when exponent/bases are integers/decimals or positive/negative // pow function is contained in math.h library #include<iostream> #include<math.h> using namespace std; int main() { double base = 1.5; double expo1 = 2.5; double expo2 = -2.5; double res1, res2; // calculates the power res1 = pow(base, expo1); res2 = pow(base, expo2); cout << base << " ^ " << expo1 << " = " << res1 << endl; cout << base << " ^ " << expo2 << " = " << res2 << endl; return 0; }
Output
1.5 ^ 2.5 = 2.75568
1.5 ^ -2.5 = 0.362887
Method 2 (without inbuilt function)
Sometimes, the interviewer may ask to calculate power without using the inbuilt function. We can do so in the following way but it has some trade-offs.
Only works when –
- When exponent is positive
- When exponent is integer
Code
Run
// Only works when exponent is positive integer #include<iostream> using namespace std; int main() { double base = 1.5; // Works only when exponent is positive integer int expo = 2; double res = 1.0; while (expo != 0) { res *= base; expo--; } cout << "Result = " << res; return 0; }
Output
Result = 2.25
Method 3 (without inbuilt function)
We solve the previous issue where code wasn’t able to handle negative exponent. But it still has the following issue, it doesn’t work when –
- When exponent is decimal
Code
Run
// Only works when exponent is integer #include<iostream> using namespace std; int main() { double base = 1.5; int expo = -2; double res = 1.0; while (expo > 0) { res *= base; expo--; } while (expo < 0) { res /= base; expo++; } cout << "Result = " << res; return 0; }
Output
Result = 0.444444
Method 4 (Using Recursion)
This method works on recursion in C++. But it still has the following issue, it doesn’t work when –
- When exponent is decimal
Code
Run
// Only works when exponent is integer #include<iostream> using namespace std; double power(double base, int exp) { if (exp > 0) return (power(base, exp - 1) * base); else if (exp < 0) return (power(base, exp + 1) / base); else return 1; } int main() { double base = 1.5; int expo = -2; double res = 1.0; cout << "Result = " << power(base, expo); return 0; }
Output
Result = 0.444444
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
#include
using namespace std;
int main()
{
int x, y, i, pro=1;
cout<> x >> y;
for (i=1; i<=y; i++)
pro= pro* x;
cout<< "\n final value= " <<pro;
return 0;
}
https://prepinsta.com/discord/
Kindly refer this link.
//using loop
#include
using namespace std;
int main()
{
int base,power;
cin>>base>>power;
int result=1;
for(int i=1;i<=power;i++)
result=result*base;
cout<<result;
return 0;
}