Answer to Question #26145 in C++ for Alpha

Question #26145
write a C++ program that uses a while loop to both display each term and determine the sum of a geometric series having a=1, r=0.5, n=10. Make sure your program displays the value it has calculated.
a+ ar + apow(r, 2) + ... + arpow(r, n-1)
1
Expert's answer
2013-03-12T12:42:07-0400
#include <cstdio>

using namespace std;

int main(int argc, char *argv[])
{
double a = 1, r = 0.5, s = 0;
int n = 10;
for (int i = 0; i < n; i++)
{
& printf("a_%d = %lf\n", i, a); //display current term
& s += a; // add it to sum
& a *= r; // calculate next term
}
printf("s = %lf\n", s); // display total sum

return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS