Sunday, August 2, 2009

Need help making a power function in C using loops. This is what i got so far.?

#include %26lt;stdio.h%26gt;


#include %26lt;stdlib.h%26gt;





void float DP(double x, int n)


{


double x, DP;


int n;





DP=pow(x,n);


printf("%d to the %i power is %d", x, n, DP);








}








int main()


{


float x, DP;


int n;





scanf("%d%i", x, n);


DP(x, n);





printf("Answer equals %d\n", DP);





return 0;


}

Need help making a power function in C using loops. This is what i got so far.?
double DP(double x, int n)


{


double result = x;





for(int i = 1; i %26lt; n; i++)


{


result *= x;


}





return(result);


}
Reply:Oops, misread what you said in my first response -- you said you need to use loops. Very well:





double DP(double x, int n) {





double result = 1.0;


for(int i = 0; i %26lt; n; i++) {


result *= x;


}





printf("%f to the %d power = %f\n", x,n,result);





return result;


}


No comments:

Post a Comment