fork download
  1. #include <stdio.h>
  2.  
  3. /*****************************************************************
  4. ** Function: Calculate_Simple_Interest
  5. **
  6. ** Description: Simple Interest is the amount of interest
  7. ** calculated by using the formula:
  8. **
  9. ** interest = (P * R * T)
  10. **
  11. ** where P is the principle, r is the rate, and t
  12. ** is the time of the investment
  13. **
  14. ** This function will return the simple interest
  15. ** calculated based upon it being passed the principle,
  16. ** rate, and time.
  17. **
  18. ** Parameters: Principle - The original principal to start with
  19. ** Rate - The rate of interest. If you wanted
  20. ** 9.5 percent it would be passed as 0.095
  21. ** Time - The time in years
  22. **
  23. ** Returns: Interest - The amount of simple interest earned
  24. **
  25. ********************************************************************/
  26.  
  27. float Calculate_Simple_Interest (float principle, float rate, float time)
  28. {
  29.  
  30. // Step 1: Define local variable of type float to hold interest
  31. float interest;
  32.  
  33. // Step 2: Calculate simple interest
  34. interest = principle * rate * time;
  35.  
  36. // Step 3: Return the calculated interest
  37. return interest;
  38.  
  39.  
  40. } /* end Calculate_Simple_Interest */
  41.  
  42. float Calculate_Compound_Interest(float principle, float rate, float time)
  43. {
  44.  
  45. // Step 1: Define local variable of type float to hold compound interest
  46. float compound_interest;
  47.  
  48. // Step 2: Calculate compound interest
  49. compound_interest = principle * (pow((1 + rate), time) - 1);
  50.  
  51. // Step 3: Return the calculated compound interest
  52. return compound_interest;
  53. }
  54.  
  55. int main (void)
  56. {
  57. float interest; /* The interest earned over a period of time */
  58. float principle; /* The amount being invested */
  59. float rate; /* The interest rate earned */
  60. float time; /* The years of the investment */
  61.  
  62. /* Initialize the interest value */
  63. interest = 0;
  64.  
  65. /* Enter values needed to determine interest */
  66. printf ("\nEnter your principle value: ");
  67. scanf ("%f", &principle);
  68.  
  69. printf ("\nEnter the rate: For example 9.5 percent would be .095: ");
  70. scanf ("%f", &rate);
  71.  
  72. printf ("\nEnter the period of time of your investment: :");
  73. scanf ("%f", &time);
  74.  
  75. /* Call simple interest function to calculate the simple interest */
  76. interest = Calculate_Simple_Interest (principle, rate, time);
  77.  
  78. /* Print the simple interest earned to the screen */
  79. printf ("\n\nThe total simple interest earned is: $%8.2f\n", interest);
  80.  
  81. /* Challenge TO DO: Step 1) Call Calculate_Compound_Interest function */
  82. /* to calculate the compound interest. */
  83.  
  84. interest = Calculate_Compound_Interest (principle, rate, time);
  85.  
  86. /* Challenge TO DO: Step 2) Print the compound interest to the screen */
  87.  
  88. printf ("\n\nThe total compound interest earned is: $%8.2f\n", interest);
  89.  
  90. return (0); /* indicate successful completion */
  91.  
  92. } /* end main */
  93.  
Success #stdin #stdout 0s 5284KB
stdin
6000
0.095
 4

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: $ 2280.00


The total compound interest earned is: $ 2625.97