# Recursive exponent

Aug 2nd, 2016
344
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. double power(double n, int p);
3. int main(void){
4.     double x, xpow;
5.     int exp;
6.
7.     printf("Enter a number and the positive integer power");
8.     printf(" to which\nthe number will be raised. Enter q");
9.     printf(" to quit.\n");
10.     while(scanf("%lf%d", &x, &exp)==2){
11.         xpow=power(x, exp);
12.         printf("%.3g to the power %d is %.5g\n", x, exp, xpow);
13.         printf("Enter next pair of numbers or q to quit.\n");
14.     }
15.
16.     printf("Hope you enjoyed this power trip...bye!\n");
17.
18.     return 0;
19. }
20.
21. double power(double n, int p){
22.     double pow=1;
23.     int i=(p<0)?-p:p;
24.     if (n==0){
25.         return 0;
26.     }
27.     else if (p==0){
28.         return 1;
29.     }
30.     else{
31.         if(i>0)
32.             pow=n*power(n, i-1);
33.     }
34.
35.     if(p<0)
36.         return 1/pow;
37.     else
38.         return pow;
39. }
RAW Paste Data