Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- double power(double n, int p);
- int main(void){
- double x, xpow;
- int exp;
- printf("Enter a number and the positive integer power");
- printf(" to which\nthe number will be raised. Enter q");
- printf(" to quit.\n");
- while(scanf("%lf%d", &x, &exp)==2){
- xpow=power(x, exp);
- printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
- printf("Enter next pair of numbers or q to quit.\n");
- }
- printf("Hope you enjoyed this power trip...bye!\n");
- return 0;
- }
- double power(double n, int p){
- double pow=1;
- int i=(p<0)?-p:p;
- if (n==0){
- return 0;
- }
- else if (p==0){
- return 1;
- }
- else{
- if(i>0)
- pow=n*power(n, i-1);
- }
- if(p<0)
- return 1/pow;
- else
- return pow;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement