A C++ program to calculate the power of any number using a function
double power(double n,int p);
using namespace std;
int main ()
{
double num,pwr;
int p;
cout<<"Input the number: "<<endl;
cin>>num;
cout<<"Input the power: "<<endl;
cin>>p;
pwr=power(num,p);
cout<<"The answer is "<<pwr<<endl;
return 0;
}
double power(double n,int p)
{
double pwr=1;
int i=1;
while(i<=p)
{
i++;
pwr=pwr*n;//The number should be multiplied by itself p times
}
return pwr;
}
Comments
Post a Comment