Simple interest and amount formulation – CSKC
Sign In

Search

Code Learning #CPP#1826    siteicon   siteicon  

Simple interest and amount formulation

Calculating simple interest and amount by using typical formulas.

Learning Objectives

  • Using the simple interest formula in its right format.
  • Using the formula for amount within the cout print chain.

Source Code

TC++ #1826

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

si=p*r*t/100;
This is typical simple interest formula, when rate of interest is given as % interest. The values input should be float or double type values else loss of precision will occur. one should not use the format 1/100 * p * r * t as it will result in 0 value. 1/100 will become 0 due numerator 1 being an integer and the value after decimal shall be ignored.

cout<<“Simple Interest = “<<si<<endl;
Here simple interest value is being printed.

cout<<“Final Amount = “<<p+si<<endl;
Here the final amount is being printed and the formula p+si is being used within the cout chain itself. This is possible when term of formula can be written in a way which will result in a single deduced value.

Notes

  • The variable si can also be avoided in this code by building the simple interest also in the cout chain like this.
    cout<<“Simple Interest = “<< p*r*t/100 <<endl;
    cout<<“Final Amount = “<<p+p*r*t/100<<endl;This may be marginally more efficient in running, but the simplicity of programming is less in this case. Some times simplicity of programming may be more important to avoid programming errors at the programmer level.


Suggested Filename(s): simpleint.cpp, simple-interest.cpp



Share

Created: 20-Dec-2017 03:02 | Updated: 20-Jan-2018 17:45|