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 a local variable to hold interest
  31. float interest;
  32.  
  33. // Step 2: Calculate simple interest
  34. interest = principle * rate * time;
  35.  
  36. // Step 3: Return the interest
  37. return interest;
  38. } /* end Calculate_Simple_Interest */
  39.  
  40.  
  41. /*****************************************************************
  42. ** Function: Calculate_Compound_Interest
  43. **
  44. ** Description: Compound Interest is calculated by the formula:
  45. **
  46. ** amount = principle * (1 + rate)^time
  47. ** compound_interest = amount - principle
  48. **
  49. ** Parameters: Principle - The original principal to start with
  50. ** Rate - The rate of interest (e.g. 0.095 for 9.5%)
  51. ** Time - The time in years
  52. **
  53. ** Returns: Compound Interest earned
  54. ********************************************************************/
  55. float Calculate_Compound_Interest(float principle, float rate, float time)
  56. {
  57. float amount = principle * pow(1 + rate, time);
  58. return amount - principle;
  59. }
  60.  
  61. int main (void)
  62. {
  63. float interest; /* The interest earned over a period of time */
  64. float principle; /* The amount being invested */
  65. float rate; /* The interest rate earned */
  66. float time; /* The years of the investment */
  67.  
  68. /* Initialize the interest value */
  69. interest = 0;
  70.  
  71. /* Enter values needed to determine interest */
  72. printf ("\nEnter your principle value: ");
  73. scanf ("%f", &principle);
  74.  
  75. printf ("\nEnter the rate: For example 9.5 percent would be .095: ");
  76. scanf ("%f", &rate);
  77.  
  78. printf ("\nEnter the period of time of your investment: :");
  79. scanf ("%f", &time);
  80.  
  81. /* Call simple interest function to calculate the simple interest */
  82. interest = Calculate_Simple_Interest (principle, rate, time);
  83.  
  84. /* Print the simple interest earned to the screen */
  85. printf ("\n\nThe total simple interest earned is: $%8.2f\n", interest);
  86.  
  87. /* Challenge TO DO: Step 1) Call Calculate_Compound_Interest function */
  88. /* to calculate the compound interest. */
  89.  
  90. /* Challenge TO DO: Step 2) Print the compound interest to the screen */
  91.  
  92. return (0); /* indicate successful completion */
  93.  
  94. } /* end main */
  95.  
Success #stdin #stdout 0.01s 5312KB
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