fahimkamal63

Sum of series

Sep 21st, 2019
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. //  Sum of series
  2. //  Date : 21.09.19
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. long long power(int num, int p){
  7.     long long result = 1;
  8.     for(int i = 1; i <= p; i++){
  9.         result *= num;
  10.     }
  11.     return result;
  12. }
  13.  
  14. void series1(int n){
  15.     long long total = 1;
  16.     cout << "The 1st series: \n\n";
  17.     for(int i = 1; i <= n; i++){
  18.         cout << i <<"^2";
  19.         if(i != n) { cout << " X "; }
  20.         total *= power(i, 2);
  21.     }
  22.     cout << " = " << total << endl << endl;
  23. }
  24.  
  25. void series2(int n){
  26.     long long total = 0, num = 3;
  27.     cout << "The 2nd series: \n\n";
  28.     for(int i = 0; i < n; i++){
  29.         cout << num;
  30.         if(i != n-1) cout << " + ";
  31.         total += num;
  32.         num += 3;
  33.     }
  34.     cout << " = " << total << endl << endl;
  35. }
  36.  
  37. void series3(int n){
  38.     long long total = 0;
  39.     cout << "The 3rd series: \n\n";
  40.     for(int i = 1; i <= n; i++){
  41.         cout << i <<"^3 X " << (i+1);
  42.         if(i != n) { cout << " + "; }
  43.         total += (power(i, 3) * (i + 1));
  44.     }
  45.     cout << " = " << total << endl << endl;
  46. }
  47.  
  48. int main(){
  49.     int n;
  50.     cout << "Enter The range of series: "; cin >> n;
  51.     series1(n);
  52.     series2(n);
  53.     series3(n);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment