fork download
  1. //Amari Mosley CSC5 Chapter 3, P. 143, #16
  2. //
  3. /**************************************************************
  4.  *
  5.  * Interest Earned
  6.  * ____________________________________________________________
  7.  * This program calculates the interest earned on a savings
  8.  * account over one year.
  9.  * Computation is based on the following formula:
  10.  * Amount = Principal * (1 + Rate / T)^T
  11.  * ____________________________________________________________
  12.  * INPUT
  13.  * principal, rate, timesCompounded
  14.  * OUTPUT
  15.  * interest, amount
  16.  *
  17.  **************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. #include <cmath>
  21. using namespace std;
  22.  
  23. // Defining Main Function
  24. int main() {
  25. // Define variables
  26. double principal, rate, amount, interest;
  27. int timesCompounded;
  28.  
  29. // Ask for the principal, interest rate, and times compounded
  30. cout << "Enter the principal balance: ";
  31. cin >> principal;
  32.  
  33. cout << "Enter the interest rate (as a percentage, e.g., 4.25): ";
  34. cin >> rate;
  35.  
  36. cout << "Enter the number of times the interest is compounded during a year: ";
  37. cin >> timesCompounded;
  38.  
  39. // Calculate the amount and interest[cite: 6]
  40. // The rate must be converted from a percentage to a decimal for the formula
  41. double rateDecimal = rate / 100.0;
  42. amount = principal * pow(1 + (rateDecimal / timesCompounded), timesCompounded);
  43. interest = amount - principal;
  44.  
  45. // Display the report formatted to match the required output[cite: 6]
  46. cout << "\n";
  47. cout << left << setw(20) << "Interest Rate:" << right << setw(7) << rate << "%" << endl;
  48. cout << left << setw(20) << "Times Compounded:" << right << setw(7) << timesCompounded << endl;
  49.  
  50. // Format numeric output for currency and align the decimals[cite: 6]
  51. cout << fixed << setprecision(2);
  52. cout << left << setw(20) << "Principal:" << "$ " << right << setw(7) << principal << endl;
  53. cout << left << setw(20) << "Interest:" << "$ " << right << setw(7) << interest << endl;
  54. cout << left << setw(20) << "Amount in Savings:" << "$ " << right << setw(7) << amount << endl;
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Enter the principal balance: Enter the interest rate (as a percentage, e.g., 4.25): Enter the number of times the interest is compounded during a year: 
Interest Rate:      4.66203e-310%
Times Compounded:      5250
Principal:          $    0.00
Interest:           $    0.00
Amount in Savings:  $    0.00