0

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();
}
2
Contributors
2
Replies
31
Views
4 Years
Discussion Span
Last Post by 2384443
0

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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.