fork download
  1. #include <stdio.h>
  2. #include <math.h> /* for the pow function */
  3.  
  4. /*****************************************************************
  5. ** Function: Calculate_Simple_Interest
  6. **
  7. ** Description: Simple Interest is the amount of interest
  8. ** calculated by using the formula:
  9. **
  10. ** interest = (P * R * T)
  11. **
  12. ** where P is the principle, r is the rate, and t
  13. ** is the time of the investment
  14. **
  15. ** This function will return the simple interest
  16. ** calculated based upon it being passed the principle,
  17. ** rate, and time.
  18. **
  19. ** Parameters: Principle - The original principal to start with
  20. ** Rate - The rate of interest. If you wanted
  21. ** 9.5 percent it would be passed as 0.095
  22. ** Time - The time in years
  23. **
  24. ** Returns: Interest - The amount of simple interest earned
  25. **
  26. ********************************************************************/
  27.  
  28. float Calculate_Simple_Interest (float principle, float rate, float time)
  29. {
  30.  
  31. /* You will only need to create three simple statements here ... */
  32. /* No other statements are needed. */
  33.  
  34. /* TO DO: Step 1) Define local variable of type float to hold interest */
  35. float interest;
  36.  
  37.  
  38. /* TO DO: Step 2) Calculate simple interest earned. Determine interest */
  39. /* using the values in the parameters: principle, rate, and time .... */
  40. /* and assign that value to the local variable you created in step 1 */
  41. /* that holds the interest */
  42. /* .... Hint: This statement is right in the first homework */
  43. interest = principle * rate * time;
  44.  
  45. /* TO DO: Step 3) Add a return statement to return the interest to main */
  46. return interest;
  47.  
  48. } /* end Calculate_Simple_Interest */
  49.  
  50.  
  51. /*****************************************************************
  52. ** Function: Calculate_Compound_Interest
  53. **
  54. ** Description: Compound Interest is the amount of interest
  55. ** calculated by using the formula:
  56. **
  57. ** interest = (P * pow (1.0 + r, t)) - P
  58. **
  59. ** where P is the principle, r is the rate, and t
  60. ** is the time of the investment
  61. **
  62. ** This function will return the compound interest
  63. ** calculated based upon it being passed the principle,
  64. ** rate, and time.
  65. **
  66. ** Parameters: Principle - The original principal to start with
  67. ** Rate - The rate of interest. If you wanted
  68. ** 9.5 percent it would be passed as 0.095
  69. ** Time - The time in years
  70. **
  71. ** Returns: Interest - The amount of compound interest earned
  72. **
  73. ********************************************************************/
  74.  
  75. float Calculate_Compound_Interest (float principle, float rate, float time)
  76. {
  77. /* Challenge Step 1) define a local float variable for interest */
  78. float interest;
  79.  
  80. /* Challenge TO DO: Step 2) Calculate compound interest earned */
  81. /* by setting interest variable using formula above */
  82. interest = (principle * pow(1.0 + rate, time)) - principle;
  83. /* Challenge Step 3) return interest to the calling function */
  84. return (interest);
  85.  
  86. } /* end Calculate_Compound_Interest */
  87.  
  88. int main (void)
  89. {
  90. float interest; /* The interest earned over a period of time */
  91. float principle; /* The amount being invested */
  92. float rate; /* The interest rate earned */
  93. float time; /* The years of the investment */
  94.  
  95. /* Initialize the interest value */
  96. interest = 0;
  97.  
  98. /* Enter values needed to determine interest */
  99. printf ("\nEnter your principle value: ");
  100. scanf ("%f", &principle);
  101.  
  102. printf ("\nEnter the rate: For example 9.5 percent would be .095: ");
  103. scanf ("%f", &rate);
  104.  
  105. printf ("\nEnter the period of time of your investment: :");
  106. scanf ("%f", &time);
  107.  
  108. /* Call simple interest function to calculate the simple interest */
  109. interest = Calculate_Simple_Interest (principle, rate, time);
  110.  
  111. /* Print the simple interest earned to the screen */
  112. printf ("\n\nThe total simple interest earned is: $%8.2f\n", interest);
  113.  
  114. /* Challenge TO DO: Step 1) Call Calculate_Compound_Interest function */
  115. /* to calculate the compound interest. */
  116. interest = Calculate_Compound_Interest (principle, rate, time);
  117.  
  118. /* Challenge TO DO: Step 2) Print the compound interest to the screen */
  119. printf ("\n\nThe total compound interest earned is: $%8.2f\n", interest);
  120.  
  121. return (0); /* indicate successful completion */
  122.  
  123. } /* end main */
  124.  
Success #stdin #stdout 0s 5340KB
stdin
4500 
0.095 
6
stdout
Enter your principle value: 
Enter the rate: For example 9.5 percent would be .095: 
Enter the period of time of your investment: :

The total simple interest earned is: $ 2565.00


The total compound interest earned is: $ 3257.06