fork download
  1. /*******************************************************************************************************
  2. Title: Calculate Simple Interest Example in C
  3. Name: Tim Niesen
  4. Description:
  5. This formula and example was taken from the about.com Mathematics web site, which
  6. has lots of useful information and formulas.
  7. URL: http://m...content-available-to-author-only...t.com/od/businessmath/ss/Interest.htm
  8. The amount of interest can be calculated by using the formula: I = Prt
  9. - where P is the principle, r is the rate, and t is the time of the investment
  10. As an example, say we had to borrow or invest an amount of $4500. The bank gives us
  11. an interest rate of 9.5% over a period of 6 years. So, let's plug in the values, and we get:
  12. I = (4500) (0.095) * (6)
  13. I = $2565.00
  14. ***********************************************************************************************************/
  15.  
  16. #include <stdio.h>
  17. int main (void)
  18. {
  19.  
  20. float interest; /* The interest earned over a period of time */
  21. float principle; /* The amount being invested */
  22. float rate; /* The interest rate earned */
  23. float time; /* The years of the investment */
  24.  
  25. /* Enter values needed to determine interest */
  26.  
  27. printf ("Enter your principle value: ");
  28. scanf ("%f", &principle);
  29. printf("\n"); /* new line */
  30.  
  31. printf ("Enter the rate: For example 9.5 percent would be .095: ");
  32. scanf ("%f", &rate);
  33. printf("\n"); /* new line */
  34.  
  35. printf ("Enter the period of time of your investment: ");
  36. scanf ("%f", &time);
  37. printf("\n"); /* new line */
  38.  
  39. /* If any value inputted is zero, then the interest is zero */
  40. if (principle == 0 || rate == 0 || time == 0)
  41. {
  42. printf("\nSince at least one of your values is zero, your interest will be zero\n");
  43. printf("... next time, make sure all values entered are non-zero!\n");
  44.  
  45. interest = 0;
  46. }
  47. else
  48. {
  49. /* calculate simple interest earned */
  50. interest = principle * rate * time;
  51. }
  52.  
  53. /* print the interest earned to the screen */
  54. printf("\n\nThe total interest earned is: $%8.2f\n", interest);
  55.  
  56. return (0); /* indicate successful completion */
  57.  
  58. } /* end main */
  59.  
Success #stdin #stdout 0s 5360KB
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 interest earned is: $ 2565.00