Advertisement
DevilDaga

Exponent_Recursive

Mar 26th, 2014
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. double exp1(int base, int power);
  5.  
  6. void exp(int base, int power, int backup)
  7. {
  8.     if (backup != 0)
  9.     {
  10.         exp1(base, power);
  11.         exp(base, power, backup - 1);
  12.     }
  13. }
  14.  
  15. double exp1(int base, int power)                    //By incrementing
  16. {
  17.     static double expo = 1;
  18.     if (power != 0)
  19.     {
  20.         exp(base, power - 1, base);
  21.         return expo += base - 1;        //Debugging
  22.     }
  23.     return 1;
  24. }
  25.  
  26. double exp2(int power, int base)                //By multiplying
  27. {
  28.     if (power == 0)
  29.         return 1;
  30.     return base * exp2(power - 1, base);
  31. }
  32.  
  33. void main()
  34. {
  35.     int base, power, choice;
  36.     printf("Enter the base and power:\n");
  37.     scanf("%d %d", &base, &power);
  38.     redo:
  39.     printf("1: Increment\n2: Multiply?\n");
  40.     scanf("%d",&choice);
  41.     switch (choice)
  42.     {
  43.     case 1:
  44.         printf("%.0f", exp1(base, power));
  45.         break;
  46.     case 2:
  47.         printf("%.0f", exp2(base, power));
  48.         break;
  49.     default:
  50.         printf("INVALID CHOICE.\n\n");
  51.         goto redo;
  52.     }
  53.     getch();
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement