Simple Interest function with default parameter values. – CSKC
Sign In

Search

Solved Problem! #CPP#2661    siteicon   siteicon  

Problem Statement - Simple Interest function with default parameter values.

Write output of the following program. Please do take care about the default parameters given while calling the function.

TC++ #2661

 

×

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

Solution


float interest(float p,int t=1,float r= 0.10);
This function declaration will set default time t to 1 and rate r to 0.10 (10%). Principal amount will be taken from the value passed to the function.

float x=1000,z=0.20;
int y=3;
cout<<interest(x,y,z)<<endl;
Here all the parameters shall be passed from values initialised before it. Output as per calculation given will be 600.

cout<<interest(5000)<<endl;
Here principal has been passed as 5000 but t and r are skipped so they will come from the default value as per declaration which is t=1 and r=0.10. So the calculation will give 500.

cout<<interest(5000,2)<<endl;
Here principal amount is 5000, t is passed as 2 years but rate will be default 0.10. So calculation will give 1000.

cout<<interest(5000,2,0.3)<<endl;
Here all the values have been passed, thus default values will not come into picture. The calculation as per actual parameters will be 3000.

float interest(float p,int t,float r) {return (p*r*t);}
The function simply return the multiplication of p r ant t. This is basically the value of simple interest.


Share

Created: 16-Jan-2018 13:56 | Updated: 16-Jan-2018 14:24|