i need help in this program. The program question is: A function power which will calculate a number m to the power n (m^n). You will pass two parameters float & int. If the parameter n is omitted then it should taken as 2.

i hope u all got the question.. i did the program but here it is going reverse, what i mean is 6^2 should return me the ans 36 but i'm getting the ans 64.. Y..?? Plz help..!!

#include<iostream.h>
#include<conio.h>
#include<math.h>
float power(float m, float n=2.0)
{
float p=1;
int i;
for(i=1;i<=m;i++)
{
p=p*n;
}
return p;
}
void main()
{
float n,z;
int m;
clrscr();
cout<<"\n Enter the value of m: \n";
cin>>m;
cout<<"\n Enter the value of n: \n";
cin>>n;
    if(n==0)
    {
    z=power(m);
    cout<<"\n The resultant value is: \n"<<z;
    }
    else
    {
    z=power(m,n);
    cout<<"\n The resultant value is: \n"<<z;
    }
getch();
}

Recommended Answers

All 2 Replies

Your power function takes n and multiplies it with itself m times. What it should be doing is take m and multiply it with itself n times.

Also n should be an int, not a float.

Thanks..!! now my program is running successfully..!! Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.