fork download
  1. #include <stdio.h>
  2.  
  3. /* Function prototypes */
  4. long int calculate_power (int number, int power);
  5.  
  6. /******************************************************/
  7. /* Function: calculate_power */
  8. /* */
  9. /* Overview: Raises an integer to a positive integer */
  10. /* power. */
  11. /* */
  12. /* Parameters: number - a integer */
  13. /* power - a positive power to raise */
  14. /* */
  15. /* Written by: Timothy Niesen */
  16. /* */
  17. /* Date Written: 10/22/91 */
  18. /******************************************************/
  19.  
  20. long int calculate_power (int number, int power)
  21. {
  22. long int result = 1; /* the result of number raised to power */
  23.  
  24. /* anything to the zero power is 1 */
  25. if (power == 0)
  26. return(result);
  27.  
  28. else
  29. {
  30. /* Here is the recursive call ... function calling itself */
  31. result = number * calculate_power(number, power - 1);
  32. } /* else */
  33.  
  34. return (result); /* to calling function */
  35.  
  36. } /* calculate_power */
  37.  
  38. int main (void)
  39. {
  40.  
  41. /* Call this function with 4 different arguments */
  42.  
  43. printf("Passed with 5,0 is %li\n", calculate_power(5,0));
  44. printf("Passed with 5,1 is %li\n", calculate_power(5,1));
  45. printf("Passed with 5,2 is %li\n", calculate_power(5,2));
  46. printf("Passed with 5,3 is %li\n", calculate_power(5,3));
  47.  
  48. return(0);
  49.  
  50. } /* main */
  51.  
Success #stdin #stdout 0s 5244KB
stdin
Standard input is empty
stdout
Passed with 5,0 is 1
Passed with 5,1 is 5
Passed with 5,2 is 25
Passed with 5,3 is 125