fork download
  1. #include <stdio.h>
  2. #include <math.h>
  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 = (principle * rate * time)
  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, calculated based
  16. ** Upon it being passed the principle, rate, and time.
  17. **
  18. ** Parameters: Principle - The original principal to start with
  19. ** Rate - The rate of interest. Example: 9.5% passed as 0.095
  20. ** Time - The time in years
  21. **
  22. ** Returns: Interest - The amount of simple interest earned
  23. **
  24. ********************************************************************/
  25.  
  26. // functions
  27. float Calculate_Simple_Interest(float principle, float rate, float time);
  28. float Calculate_Compound_Interest(float principle, float rate, float time);
  29.  
  30. // Function to calculate simple interest
  31. float Calculate_Simple_Interest(float principle, float rate, float time)
  32. {
  33.  
  34. /* You will only need to create three simple statements here ... */
  35. /* No other statements are needed. */
  36.  
  37. /* TO DO: Step 1) Define a local variable of type float to hold interest */
  38. /* ... Hint: This statement is the same of Step 1 in the challenge template */
  39.  
  40. /* TO DO: Step 2) Calculate simple interest earned - Determine the */
  41. /* interest using the values in the parameters: principle, rate, and time ... */
  42. /* and assign that value to the local variable you created in step 1 */
  43. /* that holds the interest */
  44. /* .... Hint: This statement is right in the first homework */
  45.  
  46. /* TO DO: Step 3) Add a return statement to return the interest to main */
  47. /* ... Hint: This statement is the same of Step 3 in the challenge template */
  48.  
  49. float interest;
  50. interest = principle * rate * time;
  51. return interest;
  52. }
  53.  
  54. //calculate compound interest
  55. float Calculate_Compound_Interest(float principle, float rate, float time)
  56. {
  57. float interest;
  58. interest = (principle * pow(1.0 + rate, time)) - principle;
  59. return interest;
  60. }
  61.  
  62. int main(void)
  63. {
  64. float interest; /* The interest earned over a period of time */
  65. float principle; /* The amount being invested */
  66. float rate; /* The interest rate earned */
  67. float time; /* The years of the investment */
  68. float interest_compound;
  69.  
  70. printf("\nEnter your principle value: ");
  71. scanf("%f", &principle);
  72.  
  73. printf("\nEnter the rate (e.g. 0.095 for 9.5 percent): ");
  74. scanf("%f", &rate);
  75.  
  76. printf("\nEnter the time in years: ");
  77. scanf("%f", &time);
  78.  
  79. interest = Calculate_Simple_Interest(principle, rate, time);
  80.  
  81. printf("\nThe total simple interest earned is: $%.2f\n", interest);
  82.  
  83. /* Challenge: If you decide to do the challenge, you will need to add code */
  84. /* for these two statements ... otherwise, just leave them as is */
  85. /* Challenge TO DO: Step 1) Call Calculate_Compound_Interest */
  86. /* function to calculate compound interest */
  87.  
  88. /* Challenge TO DO: Step 2) Print the compound interest to the screen */
  89.  
  90. interest = Calculate_Simple_Interest(principle, rate, time);
  91. interest_compound = Calculate_Compound_Interest(principle, rate, time);
  92.  
  93. printf("The total compound interest earned is: $%.2f\n", interest_compound);
  94.  
  95. return 0;
  96. }
Success #stdin #stdout 0s 5288KB
stdin
4500
0.095
6
stdout
Enter your principle value: 
Enter the rate (e.g. 0.095 for 9.5 percent): 
Enter the time in years: 
The total simple interest earned is: $2565.00
The total compound interest earned is: $3257.06