WAP in C++ & Python to Find the Sum of Series 1^2+3^2+5^2+…..+n^2

By | September 7, 2019

Here in the program, we will code to find the sum of all the odd number squares from 1 to n.

If the user enters an even number as the last number the sum of all the odd sq. will continue till n-1.

C++ Program to find the Sum of Series 1^2+3^2+5^2+…..+n^2:

```#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int sum=0,n,i;
cout<<"Here we will calculate the sum of series 1^2+ 3^2 + 5^2+ ...+ n^2 \n";
cout<<"Enter the value of n: ";
cin>>n;
for(i=1;i<=n;i+2)
{
sum += pow(i,2);
}
cout<<"The sum of the series is: "<<sum;
getch();
}```

Output:

```Here we will calculate the sum of series 1^2 +3^2 +5^2 +……+ n^2

Enter the value of n: 12
The sum of the series is: 286```

Python Program to find the Sum of Series 1^2+3^2+5^2+…..+n^2:

```import math
sum=0
print("Here we will calculate the sum of series 1^2 +3^2 +5^2 +……+ n^2")
n = int(input("Enter the value of n: "))
for i in range(1,n+1,2):
sum+=math.pow(i,2)
print("The sum of the series is:", int(sum))```

Output:

```Here we will calculate the sum of series 1^2 +3^2 +5^2 +……+ n^2

Enter the value of n: 9
The sum of the series is: 165```