Question

Using #include <stdio.h> 6) Write a program that computes X^Y (X to the power of Y)....

Using #include <stdio.h>
6) Write a program that computes X^Y (X to the power of Y). You are required to use a while loop to solve the problem. Do not use the pow function from the “math.h” library. You may hard code the variables X and Y in the beginning of your program.
Hint: 5^4 =5*5*5*5

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE IN C:

#include <stdio.h>

//function to calculate x to the power y and return it

double power(int x, int y)

{

//res stores x to power of absolute value of y

int res=1,copy=y;

if(y<0)

copy=-copy; //storing absolute value of y in copy

//calculating x to power of absolute value of y

while(copy!=0)

{

res*=x;

copy-=1;

}

if(y<0) //if y is negative, we return 1/(x to power of absolute value of y)

return 1.0/res;

else //else we return x to power of absolute value of y

return res;

}

int main(void)

{

//testing function

printf("2 to the power 4: %.2f\n",power(2,4));

printf("3 to the power -2: %.2f\n",power(3,-2));

printf("4 to the power 0: %.2f\n",power(4,0));

return 0;

}

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Using #include <stdio.h> 6) Write a program that computes X^Y (X to the power of Y)....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT