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