Function of an integer raised in a power


SUBMITTED BY: davindran21

DATE: Oct. 12, 2016, 1:51 a.m.

FORMAT: Text only

SIZE: 1.6 kB

HITS: 3681

  1. /***********************************************************
  2. * Function of an integer raised in a power
  3. * and the calculation of the sum of a series of integers (1^e+2^e....+n^e) raised in a power.
  4. *
  5. * Author: Spyridon Arvanitis
  6. * Date : Jan, 2014
  7. ***********************************************************/
  8. #include <stdio.h>
  9. int power(int b, int e);
  10. int main()
  11. {
  12. /* n= positive integer e= power, S= sum of integers raised in e power*/
  13. int e,n,i,S=0;
  14. /*Input e and n. Control that the inputs are positive numbers*/
  15. printf (\\"Enter the last number (n) of the series\n\\");
  16. scanf (\\"%d\\", &n);
  17. if (n<=0) /* if n is a negative number*/
  18. {
  19. printf (\\"Error. You gave a negative number. Program ends.\n\\");
  20. return 0;
  21. }
  22. printf (\\"Input the number to be the power\n\\");
  23. scanf (\\"%d\\", &e);
  24. if (e<=0) /* if n is a negative number*/
  25. {
  26. printf (\\"Error. You gave a negative number. Program ends.\n\\");
  27. return 0;
  28. }
  29. /* Calling function power n times*/
  30. for (i=1; i<=n; i++)
  31. {
  32. S += power (i,e);
  33. }
  34. printf (\\"The sum of the 1-%d series of integers raised in %d is %d\\", n,e, S);
  35. return 0;
  36. }
  37. /*Fuction to calculate a number raised in power*/
  38. int power(int b, int e)
  39. {
  40. int power=1;
  41. int i;
  42. if (b<=0)
  43. {
  44. return -1;
  45. }
  46. if (e<=0)
  47. {
  48. return -1;
  49. }
  50. for (i=1; i<=e;i++)
  51. {
  52. power*=b;
  53. }
  54. return (power);
  55. }

comments powered by Disqus