fork download
  1. //*******************************************************
  2. //
  3. // Homework: 2
  4. //
  5. // Name: Naomi Jones
  6. //
  7. // Class: Survey, Summer 2025
  8. //
  9. // Date: June 3, 2025
  10. //
  11. // Description: Program which determines simple and
  12. // compound interest.
  13. //
  14. //
  15. //********************************************************
  16.  
  17. #include <stdio.h>
  18. #include <math.h>
  19.  
  20. /* function prototypes */
  21. float Calculate_Simple_Interest(float principle, float rate, float time);
  22. float Calculate_Compound_Interest(float principle, float rate, float time);
  23.  
  24. int main (void)
  25.  
  26. {
  27.  
  28. float simp_int; /* Simple interest earned over a period of time */
  29. float comp_int; /* Compund interest earned over that period. */
  30. float principle; /* The amount being invested */
  31. float rate; /* The interest rate earned */
  32. float time; /* The years of the investment */
  33.  
  34.  
  35. /* Enter values needed to determine interest */
  36. printf ("\nEnter your principle value: ");
  37. scanf ("%f", &principle);
  38.  
  39. printf ("\nEnter the rate: For example 9.5 percent would be .095: ");
  40. scanf ("%f", &rate);
  41.  
  42. printf ("\nEnter the period of time of your investment: :");
  43. scanf ("%f", &time);
  44.  
  45. /* call simple interest function to calculate the simple interest */
  46. simp_int = Calculate_Simple_Interest (principle, rate, time);
  47.  
  48. /* print the simple interest earned to the screen */
  49. printf ("\n\nThe total simple interest earned is: $%8.2f\n", simp_int);
  50.  
  51. /*call compound interest function to calculate compound interest */
  52. comp_int = Calculate_Compound_Interest (principle, rate, time);
  53.  
  54. /* print the compound interest earned to the screen */
  55. printf ("\nThe total compound interest earned is: $%8.2f\n", comp_int);
  56.  
  57. return 0; /* Indicate successful completion */
  58.  
  59. } /* end main */
  60.  
  61.  
  62. /*****************************************************************
  63. ** Function: Calculate_Simple_Interest
  64. **
  65. ** Description: Simple Interest is the amount of interest
  66. ** calculated by using the formula:
  67. **
  68. ** interest = (principle * rate * time)
  69. **
  70. ** where P is the principle, r is the rate, and t
  71. ** is the time of the investment
  72. **
  73. ** This function will return the simple interest, calculated based
  74. ** Upon it being passed the principle, rate, and time.
  75. **
  76. ** Parameters: Principle - The original principal to start with
  77. ** Rate - The rate of interest. Example: 9.5% passed as 0.095
  78. ** Time - The time in years
  79. **
  80. ** Returns: Interest - The amount of simple interest earned
  81. **
  82. ********************************************************************/
  83.  
  84. float Calculate_Simple_Interest (float principle, float rate, float time)
  85.  
  86. {
  87.  
  88. /* You will only need to create three simple statements here ... */
  89.  
  90. /* No other statements are needed. */
  91.  
  92.  
  93. /* TO DO: Step 1) Define a local variable of type float to hold interest */
  94.  
  95. /* ... Hint: This statement is the same of Step 1 in the challenge template */
  96. float simp_int;
  97.  
  98.  
  99. /* Calculate simple interest earned - Determine the */
  100. /* interest using the values in the parameters: principle, rate, and time ... */
  101. /* and assign that value to the local variable you created in step 1 */
  102. /* that holds the interest */
  103. /* .... Hint: This statement is right in the first homework */
  104.  
  105. if (principle == 0 || rate == 0 || time == 0)
  106. {
  107. printf("\nSince at least one of your values is zero, your interest will be zero\n");
  108. printf("... next time, make sure all values entered are non-zero!\n");
  109. simp_int = 0;
  110. }
  111. else
  112. {
  113. /* calculate simple interest earned */
  114. simp_int = principle * rate * time;
  115. }
  116.  
  117.  
  118. /* TO DO: Step 3) Add a return statement to return the interest to main */
  119.  
  120. /* ... Hint: This statement is the same of Step 3 in the challenge template */
  121. return (simp_int);
  122.  
  123.  
  124. } /* end Calculate_Simple_Interest */
  125.  
  126.  
  127. /* Challenge: If you decide to do the challenge, you will need to add code */
  128. /* for these two statements ... otherwise, just leave them as is */
  129.  
  130. /* Challenge TO DO: Step 1) Call Calculate_Compound_Interest */
  131.  
  132. /* function to calculate compound interest */
  133.  
  134.  
  135. /* Challenge TO DO: Step 2) Print the compound interest to the screen */
  136.  
  137.  
  138. /*****************************************************************
  139. ** Function: Calculate_Compound_Interest
  140. **
  141. ** Description: Compound Interest is the amount of interest
  142. ** calculated by using the formula:
  143. **
  144. ** interest = (P * pow (1.0 + r, t)) - P
  145. **
  146. ** where P is the principle, r is the rate, and t
  147. ** is the time of the investment
  148. **
  149. ** This function will return the compound interest
  150. ** calculated based upon it being passed the principle,
  151. ** rate, and time.
  152. **
  153. ** Parameters: Principle - The original principal to start with
  154. ** Rate - The rate of interest. If you wanted
  155. ** 9.5 percent it would be passed as 0.095
  156. ** Time - The time in years
  157. **
  158. ** Returns: Interest - The amount of compound interest earned
  159. **
  160. ********************************************************************/
  161.  
  162. float Calculate_Compound_Interest (float principle, float rate, float time)
  163. {
  164. /* Challenge Step 1) define a local float variable for interest */
  165. float comp_int;
  166.  
  167. /* Challenge TO DO: Step 2) Calculate compound interest earned */
  168. /* by setting interest variable using formula above */
  169.  
  170. /* calculate compound interest earned */
  171. comp_int = (principle * pow (1.0 + rate, time)) - principle;
  172.  
  173.  
  174. /* Challenge Step 3) return interest to the calling function */
  175. return (comp_int);
  176.  
  177. } /* end Calculate_Compound_Interest */
  178.  
  179.  
Success #stdin #stdout 0.01s 5320KB
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