fork download
  1. #include <stdio.h>
  2.  
  3. int main(void) {
  4.  
  5. int x = 3;
  6.  
  7. int n = 3;
  8.  
  9. int output = 0;
  10.  
  11. int x_to_the_n (int xth, int nth);
  12.  
  13. output = x_to_the_n(x,n);
  14.  
  15. printf ("%7i\n",output);
  16.  
  17. return 0;
  18. }
  19.  
  20. /*******************************************************************
  21. ** Function: x_to_the_n
  22. **
  23. ** Purpose: Do a calculation for exponents
  24. **
  25. ** Parameters:
  26. *
  27. ** xth the input to be raised by the exponent
  28. **
  29. ** nth the exponent
  30. **
  31. ** Returns: The exponent calculation
  32. **
  33. *******************************************************************/
  34.  
  35. x_to_the_n (int xth, int nth)
  36. {
  37. /* Set variable to do the x to the power of n */
  38. int powerCalculated = 0;
  39.  
  40. /* To count loop */
  41. int i;
  42.  
  43. /* Evaluate powers nth input */
  44. if (nth > 0) /* If greater than 0 will proceed with operation */
  45. {
  46. powerCalculated = xth; /* Sets powerCalculated to the xth, if nth is 1 then will be the return value */
  47. for (i = 0; i < nth -1 ; i++)
  48. {
  49. powerCalculated = powerCalculated * xth; /*Muplties the poweCalculated by xth */
  50. }
  51. }
  52. else if (nth == 0) /* Sets powerCaculated to 1 if nth value is 0 */
  53. {
  54. powerCalculated = 1;
  55. }
  56.  
  57. return (powerCalculated);
  58. }
  59.  
Success #stdin #stdout 0s 4280KB
stdin
Standard input is empty
stdout
     27