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