1
0

power.c 831 B

1234567891011121314151617181920212223242526272829303132
  1. // power.c -- raises numbers to integer powers
  2. #include <stdio.h>
  3. double power(double n, int p); // ANSI prototype
  4. int main(void)
  5. {
  6. double x, xpow;
  7. int exp;
  8. printf("Enter a number and the positive integer power");
  9. printf(" to which\nthe number will be raised. Enter q");
  10. printf(" to quit.\n");
  11. while (scanf("%lf%d", &x, &exp) == 2)
  12. {
  13. xpow = power(x,exp); // function call
  14. printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
  15. printf("Enter next pair of numbers or q to quit.\n");
  16. }
  17. printf("Hope you enjoyed this power trip -- bye!\n");
  18. return 0;
  19. }
  20. double power(double n, int p) // function definition
  21. {
  22. double pow = 1;
  23. int i;
  24. for (i = 1; i <= p; i++)
  25. pow *= n;
  26. return pow; // return the value of pow
  27. }