Exercise 210 only The language is C Rewrite the product func

Exercise 2.10 only. The language is C.

Rewrite the product function of Exercise 2.8 using a for loop. Write a function to compute the power a^n, where n greaterthanorequalto 0. It should have the following prototype:/* Sets *p to the n\'th power of a and returns 0, except * when n

Solution

#include<stdio.h>
#include <math.h>
int power(int x,int y, int *p)
{
int temp;
if(p==NULL || p<0)
return -1;
else
{
int i=0;
int z=1;
for(i=0;i<y;i++)
{
z=z*x;
}
*p=z;
return 0;
}

}
int main()
{
int x = 2;
int y = 3;
int pow ;
power(x, y, &pow);
printf(\"%d^%d = %d\ \", x,y,pow);
x=5,y=2;
power(x, y, &pow);
printf(\"%d^%d = %d\ \", x,y,pow);
getchar();
return 0;
}

Exercise 2.10 only. The language is C. Rewrite the product function of Exercise 2.8 using a for loop. Write a function to compute the power a^n, where n greater

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site