sum of a series of squares of squares

I am trying to get the result of this formula.. 1 + (x)2 + (x)4 + (x)6 + .....
how can i get the square to power 4 and so on? please help
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream.h>
#include <math.h>

main()
{
float x ;
int terms,counter,i,j, sqq;
double sq ;
cout<<"Enter the value of x: ";
cin>>x;
cout<<endl;
cout<<"Enter the number of terms: ";
cin>>terms;

for(counter=1; counter<=terms; counter++)
    {
     sq = counter * counter ;
     sq = pow(sq , 2);
     cout<<sq<<endl;

    }

it shows:
Enter the value of x: 2

Enter the number of terms: 3
1
16
81

delete line 17;
sorry. I mistranslated. sorry
Last edited on
double sqx = x*x;
double pows = sqx;
double total = 1;
for(counter=0; counter<terms; counter++)
{
double += pows;
pows *= sqx;
}


this is specific to your series, though. If you need a more generic approach, we can dig deeper.

Hello learner47,

I believe this is more inline with what you want.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <cmath>
#include <iomanip>
#include <limits>

int main()
{
	//  1 + (x)2 + (x)4 + (x)6 + ..... working formula.

	double base{ 0.0 };
	double total{ 1.0 }; //  Starts at 1 to work with formula.
	double exp{ 2.0 };  //  Starts at 2 because the first time the number is squared.
	size_t terms{ 0 };

	std::cout << "\n Enter the base value: ";
	std::cin >> base;
	std::cout << std::endl;
	std::cout << " Enter the number of terms: ";
	std::cin >> terms;

	for (size_t lp = 0; lp < terms; exp += 2, lp++)
	{
		std::cout << "\n " << total;
		total += pow(base, exp);
	}

	std::cout << std::fixed << std::showpoint << std::setprecision(4);
	std::cout << "\n Total = " << total << std::endl;

	std::cout << "\n\n\n\n Fin Press any key to continue -- > ";
	std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  //  Use whatever yu like to hold the screen open.
	return 0;
}   //  End main 


Hope that helps,

Andy
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
   double x, xsq, sumsq;
   int n;
   double eps = 1.0e-10;

   cout << "Input x: ";   cin >> x;
   cout << "Input n: ";   cin >> n;
   xsq = x * x;

   if ( abs( xsq - 1.0 ) < eps ) sumsq = n;                                              // xsq effectively equal to 1
   else                          sumsq = ( 1.0 - pow( xsq, n ) ) / ( 1.0 - xsq );        // sum of geometric series to n terms

   cout << "Sum is " << sumsq;
}
Last edited on
Each term in the series is the previous term times x2. So keep a variable for the current term:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using std::cin;
using std::cout;

int main()
{
    float x;
    int terms;

    cout << "Enter the value of x: ";
    cin >> x;
    cout << '\n';
    cout << "Enter the number of terms: ";
    cin >> terms;

    double xsq = x * x;		// x squared
    double curTerm = 1;		// current term
    double sum = 0.0;		// running sum
    for (int counter = 1; counter <= terms; counter++) {
	cout << curTerm << ' ';
	sum += curTerm;
	curTerm *= xsq;
    }
    cout << "\nsum is " << sum << '\n';
}

Hello everyone,

Given the formula 1 + (x)2 + (x)4 + (x)6 + ...... I took that to mean that the value of the exponent changes not the value of "x". Did I misunderstand something here?

Andy

Given the formula 1 + (x)2 + (x)4 + (x)6 + ...... I took that to mean that the value of the exponent changes not the value of "x". Did I misunderstand something here?

You've understood that correctly. x is some number that the user inputs and will not change after input.
@ integralfx,

Thank you, I was starting to think my solution was wrong.

Andy
Did I misunderstand something here?


No, I think we're all working to the same as you. Each term changes by a factor of x2 (so it's a geometric series). There's some minor differences of opinion over what "number of terms" means (is 1 the zeroth or first term?) that's all.

The OP's code is changing the value of x each time (and I'm not sure quite what his/her title is implying), so I guess he/she will have to tell us what is intended.



Hello everyone...

Thanks to all of you i got what i intended to do, all of you have given a nice approach.. thanx again
Topic archived. No new replies allowed.