/*Raising
a number n to a power p is the same as multiplying n by itself p times. Write a
function called power() that takes a double value for n and an int value for p,
and returns the result as double value.Use a default argument of 2 for p, so
that if this argument is omitted , the number will be squared. Write a main()
function that gets values from the user to test this function.*/
#include<iostream>
//Header file
double power(double , int);
// Prototype
int main()
{
int pow1;
double num1 , result;
cout<<"\n Enter the
number";
cin>>num1;
cout<<"\n Enter the
power";
cin>>pow1;
result=power(num1,pow1); // calling function
cout<<"\n power of the
entered number is :"<<result;
return 0;
}
double power(double n ,int p) //Function Definitions
{
int count;
double res=1;
if(p==0)
return(1);
else if (p<0)
{
cout<<"\n\n
negative power not allowed.Try Again.!";
exit(-1);
}
else
{
for(count=1;
count<=p; count++)
{
res=res*n;
}
return(res);
}
}
Output
Enter the number 2
Enter the power 5
Power of the entered number is :32
0 Comments