
Here's my code:
#include<iostream>
using namespace std;
double power(double, int);
int main(){
double n;
int p;
double ans;
cout<<"Enter the number first and then the power to which it is to be raised: \n";
cin>>n>>p;
ans = power(n,p);
cout<<n<<" raised to the power "<<p<< " is "<<ans<<endl;
system("pause");
return 0;
}
double power(double n, int p)
{
double total = 1;
for(int i=1;i<=p;i++)
{
total = total*n;}
if(p<0)
{return n*n;}
else
return total;
}
The code works fine and gives the correct results but I have no idea how the value of p can be omitted by the user? I mean that is there any way in which the user can NOT enter the value of p with the code that I've made? Also, how should I modify the "if" part in my function so that my code is the correct answer to the question that I have posted?
Any help would be appreciated.

New Topic/Question
Reply




MultiQuote






|