How to calculate a number with power as decimal number without using pow() function.
Here is the code to calculate the power as decimal number using pow() function.
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double num,exp;
cin>>num>>exp;// number and the power of it
cout<<"Power is: "<<pow(num,exp)<<endl;
}
Input:
2 2.5
Output:
Power is: 5.65685
But the another method without using the pow() function is given below (Improvement is accepted.)
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double num,exp;
cin>>num>>exp;
double temp = 0;
for(int i=0;i<exp;i++)
{
temp+=num;
}
cout<<"Power is: "<<temp<<endl;
return 0;
}
Input:
2 2.5
Output:
Power is: 4 (incorrect)
Finally I want to find out the power of base number (Power can be decimal value) manually without using pow() function. Thanks in advance!!.