Friday 23 January 2015

Write a C++ program for 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.


#include <iostream>
using namespace std;
double power(double n,int p=2)
{
double x=1.0;
int i;
for (i=0;i x*=n;
return x;
}
int main()
{
double m,res;
int q;
char ch;
cout<<"Enter a number to be raised to a power: ";
cin>>m;
cout<<"Do you want to enter the power (Y/N): ";
cin>>ch;
if ((ch=='y')||(ch=='Y'))
{
cout<<"Enter the power to which the number "< cin>>q;
res=power(m,q);
}
else
res=power(m);
cout<<"The result is: "<<res;
}

1 comment:

SAY HELLO!!