fork download
  1. //Jeremy Huang CS1A Chapter 3, P. 146, #16
  2. //
  3. /**************************************************************
  4.  *
  5.  * CALCULATE INTEREST EARNED
  6.  * ____________________________________________________________
  7.  * This program calculates the total amount of savings and
  8.  * interest earned after one year based on a given principal,
  9.  * interest rate, and the number of times the interest is
  10.  * compounded annually.
  11.  * ____________________________________________________________
  12.  * INPUT
  13.  * principal : intial balance in savings account
  14.  * interestRate : annual interest rate
  15.  * timesCompounded : times compounded per year
  16.  *
  17.  * OUTPUT
  18.  * interest : total interest earned in the year
  19.  * amountInSavings : final amount in savings account
  20.  *
  21.  **************************************************************/
  22. #include <iostream>
  23. #include <iomanip>
  24. #include <cmath>
  25. using namespace std;
  26.  
  27. int main() {
  28. double principal; //INPUT - intial balance in savings account
  29. double interestRate; //INPUT - annual interest rate
  30. double interest; //OUTPUT - total interest earned in the year
  31. double amountInSavings; //OUTPUT - total interest earned in the year
  32. int timesCompounded; //INPUT - times compounded per year
  33.  
  34. //User Input
  35. cout<<"Enter principal amount: ";
  36. cin>>principal;
  37. cout<<"\nEnter the annual interest rate (in percent): ";
  38. cin>>interestRate;
  39. cout<<"\nEnter the amount of times interest is compounded per year";
  40. cin>>timesCompounded;
  41.  
  42. //Calculate final amount in savings
  43. amountInSavings = principal * pow((1 + (interestRate/100.0)/
  44. timesCompounded), timesCompounded);
  45.  
  46. //Calculate how much money earned in interest
  47. interest = amountInSavings - principal;
  48.  
  49. //Output Result
  50. cout<<"\n"<<fixed<<setprecision(2);
  51. cout<<left<<setw(20)<<"Interest Rate:"<<right<<setw(10)<<setprecision(2)
  52. <<interestRate<<"%"<<endl;
  53. cout<<left<<setw(20)<<"Times Compounded:"<<right<<setw(11)<<timesCompounded
  54. <<endl;
  55. cout<<left<<setw(20)<<"Principal:"<<" $"<<right<<setw(9)<<principal<<endl;
  56. cout<<left<<setw(20)<<"Interest:"<<" $"<<right<<setw(9)<<interest<<endl;
  57. cout<<left<<setw(20)<<"Amount in Savings:"<<" $"<<right<<setw(9)
  58. <<amountInSavings<<endl;
  59. return 0;
  60. }
Success #stdin #stdout 0.01s 5320KB
stdin
1000
4.25
12
stdout
Enter principal amount: 
Enter the annual interest rate (in percent): 
Enter the amount of times interest is compounded per year
Interest Rate:            4.25%
Times Compounded:            12
Principal:           $  1000.00
Interest:            $    43.34
Amount in Savings:   $  1043.34