Program 1 – Power Function

Raising a number n to a power p is 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 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 the main function that gets the values from user the user to test the function.

Code

#include<iostream>
using namespace std;


double Power(double n, int p)
{
int i;
double t=n;
for(i=1;i<p;i++)
{
    t*=n; 
}
return t;
}
double Power(double n)
{
return n*n;
}

int main()
{
    double n;
    int p,ch;
    cout << "Enter value: " ;
    cin >> n;
    cout << "Enter 1 to enter power or 0 to use default power: ";
    cin >> ch;
    if(ch==0)
    {
     cout <<double(Power(n));   
     return 0;
    }
    cout << "Enter power: ";
    cin >> p;
    cout << double(Power(n, p));
    return 0;
    
}