fork download
  1. //Renee V. Lopez CS1A Chapter 3, Pg.146, #16
  2. /*******************************************************************************
  3.  * CALCULATE COMPOUND INTEREST
  4.  * _____________________________________________________________________________
  5.  * This program calculates the total amount in a savings account and the interest
  6.  * earned after compounding based on user input for the principal, interest rate,
  7.  * and number of times the interest is compounded per year.
  8.  *******************************************************************************/
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <cmath>
  12. using namespace std;
  13.  
  14. int main () {
  15. // Declare variables to hold user inputs and results
  16. float interestRate; // Annual interest rate as a percentage
  17. float timesCompounded; // Number of times interest is compounded per year
  18. float principal; // Initial amount in the savings account
  19. float interest; // Used for storing intermediate interest calculation
  20. float amountinSavings; // Final amount after adding interest
  21. float interestEarned; // Total interest earned
  22.  
  23. // Gather input from user
  24. cout << "Enter the Interest Rate (in percentage): " << endl;
  25. cin >> interestRate;
  26. cout << "Enter the number of times interest is compounded per year: " << endl;
  27. cin >> timesCompounded;
  28. cout << "Enter the initial principal amount: " << endl;
  29. cin >> principal;
  30.  
  31. // Perform calculations
  32. interestRate /= 100; // Convert interest rate to decimal
  33. interest = 1 + (interestRate / timesCompounded); // Calculate interest factor
  34. amountinSavings = principal * pow(interest, timesCompounded); // Final amount in savings
  35. interestEarned = amountinSavings - principal; // Calculate interest earned
  36.  
  37. // Display results with appropriate formatting
  38. cout << fixed << setprecision(2);
  39. cout << "Interest Earned: $" << interestEarned << endl;
  40. cout << "Total Amount in Savings: $" << amountinSavings << endl;
  41.  
  42. return 0;
  43.  
  44. }
Success #stdin #stdout 0s 5284KB
stdin
5
4
1000
stdout
Enter the Interest Rate (in percentage): 
Enter the number of times interest is compounded per year: 
Enter the initial principal amount: 
Interest Earned: $50.95
Total Amount in Savings: $1050.95