Develop an OOP to find the sum of “n” terms of the following series.
1^3 + 3 x 2^2 + 3^3 + 3 x 4^2 + 5^3 + 3 x 6^2 + . . . . .
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int n;
cout<<"\nEnter a number: ";
cin>>n;
int sum1=0;
for(int i=1;i<=n;i++){
if (i%2 !=0)
sum1=sum1+pow(i,3);
}
int sum2=0;
for(int i=2;i<=n;i++){
if (i%2 ==0)
sum2=sum2+(3*pow(i,2));
}
int sum=sum1+sum2;
cout<<"\nThe sum is "<<sum;
return 0;
}
Comments
Leave a comment