Find sum of series 1^2+3^2+5^2+……+n^2 – C++ program

CWC
2 Min Read

C++ program to find sum of series 1^2+3^2+5^2+……+n^2.

#include
#include
using namespace std;
int main()
{
int n,i;
long sum=0;
cout<<"1^2+3^2+5^2+......+n^2\n\n Enter Value of n:"; cin>>n;
for(i=1;i<=n;i+=2)
sum+=(i*i);
cout<<"\n Sum of given series is "<<sum;
return 0;
}

In this article, we are going to discuss the C++ program to find the sum of series 1^2+3^2+5^2+…+n^2.

Let’s say we have a series 1^2+3^2+5^2+….+n^2.

The formula for calculating the sum of this series is (1/1)+(1/3)+(1/5)+(1/7)+….+(1/n).

We have found the formula for this series. Now, we need to find the value of 1/1, 1/3, 1/5, 1/7,…, 1/n.

For this, we need to write a C++ program which can calculate the value of the above given series.

I am providing a C++ program for the same.

C++ Program to Find Sum of Series 1^2+3^2+5^2+…+n^2


#include

using namespace std;

int main()

{

int n,sum=0,i=1;

cout<<"Enter the number of terms"<<endl;< p=""></endl;<>

cin>>n;

for(i=1;i<=n;i++)

{

sum=(sum*i)/i;

}

cout<<"Sum of the series is "<<sum<<endl;< p=""></sum<<endl;<>

return 0;

}

Here, I have used the following concept.

  • We are calculating the sum for the given series and storing it into a variable.
  • The loop runs until the number of terms has been reached.
  • Inside the loop, we are multiplying the term i and dividing it by i.
  • In the end, we are getting the summation of the given series.

So, the output for the above program is:

Enter the number of terms:

2

Sum of the series is 13

Share This Article
Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *

English
Exit mobile version