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