Advertisement
Maco153

prob 3

Oct 22nd, 2019
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int factorial(int n) {
  5.     int fact = 1;
  6.     if (n == 0) {
  7.         return 1;
  8.     }
  9.     else {
  10.         for (int i = 1; i <= n; i++) {
  11.             fact = fact * i;
  12.         }
  13.         return fact;
  14.     }
  15. }
  16.  
  17. int powercalc(int base, int power) {
  18.     int result = 1;
  19.     for (int i = 1; i <= power; i++) {
  20.         result = result *base;
  21.     }
  22.     return result;
  23. }
  24. float exponent(int x, int y) {
  25.     float result = 0;
  26.     for (int i = 0; i <= y; i++) {
  27.         result = result + (powercalc(x, i)*1.0 / factorial(i));
  28.     }
  29.     return result;
  30.  
  31. }
  32. int main() {
  33.     int num = 0, n=0;
  34.     float result;
  35.     cout << "Enter the number x to get the result (e^x)!\n";
  36.     cin >> num;
  37.     cout << "Enter the number n that defines how many times you want to run the series!\n ";
  38.     cin >> n;
  39.     result = exponent(num, n);
  40.     cout << "The result after doing the series is " << result << endl;
  41.     return 0;
  42.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement