CPP program to calculate the power
In this solution try to solve, the problem of finding the power of a number manually and with pow() function in the cpp programming language.
Firstly, look at manually finding the power of any number.
Code:
#include <iostream>
using namespace std;
int main()
{
int exponent;
float base, pow = 1;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
cout << base << "^" << exponent << " = ";
for(int i=1;i<=exponent;i++){
pow *= base;
}
cout << pow;
return 0;
}
Output:
Enter base and exponent respectively: 3
3
3^3 = 27
Compute the power of the number by using pow() function from the cmath library.
code:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int exponent;
float base, powe = 1;
cout << "Enter base and exponent respectively: ";
cin >> base >> exponent;
cout << base << "^" << exponent << " = ";
powe=pow(base, exponent);
cout << powe;
return 0;
}
Output:
Enter base and exponent respectively: 3
3
3^3 = 27
Comments
Post a Comment