#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
#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
#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';
}
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?
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.
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.