Handout - Nested loops
// POWER SERIES PROGRAM
//
// This program output the series
//
// x, x/2, x/4, ..., x/2^^n
//
// for given values of x and n. The program illustrates
// nested loops and the post-decrement operator.
//
#include < iostream>
#include < cmath>
using namespace std;
int main()
{
int pairs; // number of input pairs
int i; // loop control variable
int n; // max power to raise x to
double x,
y; // the value of each term in the series
cout << "Enter number of input pairs: ";
cin >> pairs;
while (pairs--)
{
cout << "Enter x and n: ";
cin >> x >> n;
cout << endl << "Here are the first " << n+1 << " values in the series"
<< endl;
for (i = 0; i<=n; i++)
{
y = x / pow(2.0, i); // raises 2 to the ith power
cout << y << " ";
}
cout << endl << endl;
}
return 0;
}