Write C program to find power of a number using recursion


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write C program to find power of a number using recursion


  1. #include <stdio.h>
  2.  
  3. //function declaration
  4. double Power(double base, int exponent);
  5.  
  6. int main()
  7. {
  8. double base, power;
  9. int exponent;
  10.  
  11. // Inputting base and exponent from user
  12. printf("Enter base: ");
  13. scanf("%lf", &base);
  14. printf("Enter exponent: ");
  15. scanf("%d", &exponent);
  16.  
  17. // Call Power function
  18. power = Power(base, exponent);
  19.  
  20. printf("%.2lf ^ %d = %f", base, exponent, power);
  21.  
  22. return 0;
  23. }
  24.  
  25. /*
  26.   Calculating power of any number.
  27.   Returns base ^ exponent
  28.  */
  29. double Power(double base, int exponent)
  30. {
  31. // Base condition
  32. if(exponent == 0)
  33. return 1;
  34. else if(exponent > 0)
  35. return base * pow(base, exponent - 1);
  36. else
  37. return 1 / pow(base, - exponent);
  38. }
  39.  

Result

C language

     





Related Topics




Popular Posts