Write a C++ Program to Calculate Sum of Series 1³+2³+3³+n³ with an example. The math formula to find Sum of Series 1³+2³+3³+……+n³ = (n(n + 1)/6)².
#include<iostream> #include<cmath> using namespace std; int main() { int number, sum = 0; cout << "Enter the Number to find sum of Series 1^3 + 2^3 = "; cin >> number; sum = pow(((number * (number + 1)) / 2), 2); cout << "\nThe Sum of the Series 1^3 + 2^3 + .. upto " << number << " = " << sum << "\n\n"; return 0; }
Enter the Number to find sum of Series 1^3 + 2^3 = 10
The Sum of the Series 1^3 + 2^3 + .. upto 10 = 3025
The below shown C++ Program to Calculate Sum of Series 1³+2³+3³+n³ is the same as the above. Here, we used C++ for loop to display the 1³+2³+3³+n³ series as an output.
#include<iostream> #include<cmath> using namespace std; int main() { int number, sum = 0; cout << "Enter the Number to find sum of Series 1^3 + 2^3 = "; cin >> number; sum = pow(((number * (number + 1)) / 2), 2); cout << "\nThe Sum of the Series 1^3 + 2^3 + .. upto " << number << " = " << sum << "\n\n"; for(int i = 1; i <= number; i++) { if (i != number) cout << i << "^3 + "; else cout << i << "^3 = " << sum; } return 0; }
C++ Program to Calculate Sum of Series 1³+2³+3³+n³ using Functions
#include<iostream> #include<cmath> using namespace std; void sumOf13Series (int number) { int sum = 0; sum = pow(((number * (number + 1)) / 2), 2); cout << "\nThe Sum of the Series 1^3 + 2^3 + .. upto " << number << " = " << sum << "\n\n"; for(int i = 1; i <= number; i++) { if (i != number) cout << i << "^3 + "; else cout << i << "^3 = " << sum; } } int main() { int number; cout << "Enter the Number to find sum of Series 1^3 + 2^3 = "; cin >> number; sumOf13Series (number); return 0; }
Enter the Number to find sum of Series 1^3 + 2^3 = 30
The Sum of the Series 1^3 + 2^3 + .. upto 30 = 216225
1^3 + 2^3 + 3^3 + 4^3 + 5^3 + 6^3 + 7^3 + 8^3 + 9^3 + 10^3 + 11^3 + 12^3 + 13^3 + 14^3 + 15^3 + 16^3 + 17^3 + 18^3 + 19^3 + 20^3 + 21^3 + 22^3 + 23^3 + 24^3 + 25^3 + 26^3 + 27^3 + 28^3 + 29^3 + 30^3 = 216225