fork download
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. /*------------------------------------------------------------
  5.   Function: Calculate_Simple_Interest
  6.   Description: Returns simple interest using the formula
  7.   I = P * r * t
  8.   Parameters:
  9.   principle – original amount invested (P)
  10.   rate – annual interest rate as a decimal (r)
  11.   time – investment period in years (t)
  12.   Returns:
  13.   Interest earned over the period
  14. ------------------------------------------------------------*/
  15. float Calculate_Simple_Interest(float principle, float rate, float time)
  16. {
  17. /* Step 1 – local variable to hold the result */
  18. float interest = 0.0f;
  19.  
  20. /* Step 2 – compute simple interest */
  21. interest = principle * rate * time;
  22.  
  23. /* Step 3 – return the value to the caller */
  24. return interest;
  25. }
  26.  
  27. /*------------------------------------------------------------
  28.   Function: Calculate_Compound_Interest
  29.   Description: Returns compound interest using the formula
  30.   I = P*(1 + r)^t – P
  31.   Parameters:
  32.   principle – original amount invested (P)
  33.   rate – annual interest rate as a decimal (r)
  34.   time – investment period in years (t)
  35.   Returns:
  36.   Compound interest earned over the period
  37. ------------------------------------------------------------*/
  38. float Calculate_Compound_Interest(float principle, float rate, float time)
  39. {
  40. /* Challenge Step 1 – local variable */
  41. float interest = 0.0f;
  42.  
  43. /* Challenge Step 2 – compute compound interest */
  44. interest = (float)(principle * powf(1.0f + rate, time) - principle);
  45.  
  46. /* Challenge Step 3 – return the value */
  47. return interest;
  48. }
  49.  
  50. int main(void)
  51. {
  52. float principle = 0.0f;
  53. float rate = 0.0f;
  54. float time = 0.0f;
  55. float simple_interest = 0.0f;
  56. float compound_interest = 0.0f;
  57.  
  58. /* Prompt for user input */
  59. printf("Enter your principle value: ");
  60. scanf("%f", &principle);
  61.  
  62. printf("Enter the rate (e.g., 9.5%% as 0.095): ");
  63. scanf("%f", &rate);
  64.  
  65. printf("Enter the period of time of your investment (years): ");
  66. scanf("%f", &time);
  67.  
  68. /* Calculate and display simple interest */
  69. simple_interest = Calculate_Simple_Interest(principle, rate, time);
  70. printf("\nThe total simple interest earned is: $%8.2f\n", simple_interest);
  71.  
  72. /* Calculate and display compound interest */
  73. compound_interest = Calculate_Compound_Interest(principle, rate, time);
  74. printf("\nThe total compound interest earned is: $%8.2f\n", compound_interest);
  75.  
  76. return 0;
  77. }
Success #stdin #stdout 0s 5320KB
stdin
4500 
0.095 
6
stdout
Enter your principle value: Enter the rate (e.g., 9.5% as 0.095): Enter the period of time of your investment (years): 
The total simple interest earned is: $ 2565.00

The total compound interest earned is: $ 3257.06