In this Program, you will learn to find or Calculate Power of a Number manually, and by using pow( ) function.
To understand this Program to Calculate Power of a Number, you should have the knowledge of following C++ programming topics:
- C++ while and do…while Loop
This program takes two numbers from the user (a base number and an exponent) and calculates the power.
Power of a number = baseexponent
Program to Compute Power Manually
#include <iostream> using namespace std; int main() { int exponent; float base, result = 1; cout << "Enter base and exponent respectively: "; cin >> base >> exponent; cout << base << "^" << exponent << " = "; while (exponent != 0) { result *= base; --exponent; } cout << result; return 0; }
Output
Enter base and exponent respectively: 3.4 5 3.4^5 = 454.354
The above technique works only if the exponent is a positive integer.
If you need to find the power of a number with any real number as an exponent, you can use pow( ) function.
Program to Compute power using pow( ) Function
#include <iostream> #include <cmath> using namespace std; int main() { float base, exponent, result; cout << "Enter base and exponent respectively: "; cin >> base >> exponent; result = pow(base, exponent); cout << base << "^" << exponent << " = " << result; return 0; }
Output
Enter base and exponent respectively: 2.3 4.5 2.3^4.5 = 42.44
Related Programs
- C++ Programs To Create a Pyramid and Pattern
- C++ Program to Check Whether a Number is Prime or Not
- C++ Program to display Amstrong Number between Two intervals.
- C++ Program to create a Pyramid and Pattern.
- C++ Program to make a simple calculator using switch…case.
- C++ Program to Check Whether a Number is Palindrome or Not
Ask your questions and clarify your/others doubts on Calculate the Power of a Number by commenting. Documentation
Please write to us at [email protected] to contribute or to report an issue with the above content or for feedback