Program Listing

\[B = R \left[ { {1 - (x + 1) (1 + i)^{-(n-x)} } \over { 2i - 1} } \right] \]
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	cout << "Please enter R: ";
	double	R;
	cin >> R;

	cout << "Please enter x: ";
	double	x;
	cin >> x;

	cout << "Please enter i: ";
	double	i;
	cin >> i;

	cout << "Please enter n: ";
	double	n;
	cin >> n;

	double B = R * (1 - (x + 1) * pow(1 + i, -(n - x))) / (2 * i - 1);		// (a)
	//double B = R * ((1 - (x + 1) * pow((1 + i), -(n - x))) / (2 * i - 1));	// (b)
	//double B = R * (1 - (x + 1) * pow(1 + i, -n + x)) / (2 * i - 1);		// (c)

	cout << B << endl;

	return 0;
}
A complex equation implemented in C++. There are many, generally equivalent, ways that we can convert an equation into a C++ statement. The three versions included in the solution keep the C++ code close to the original equation.
  1. This version uses the minimum number of parentheses while retaining the original exponent expression. It uses precedence to eliminate the square brackets appearing in the original equation.
  2. The second version simplifies the exponent, eliminating a pair of parentheses.
  3. The last version retains the parentheses that replace the square brackets.