Do problem 16 in section 3.9 of the text (but DO NOT use the hint given there).
ID: 3587017 • Letter: D
Question
Do problem 16 in section 3.9 of the text (but DO NOT use the hint given there). Copy your account.c file from your Lab4 directory into your Lab5 directory. Instead of using the hint given in the text, if you think about this one for a minute, you can do it WITHOUT modifying the function you wrote for Lab4. (Hint: the trick is how you get the values you pass to the function calc_acc_amt(). Think about what the MEANING of the parameters are). However, you should write a new main() function which should repeatedly prompt the user to enter the data as before, and also prompt for either annual, monthly or daily compounding. This should be entered as an integer - 1 for annual, 2 for monthly, and 3 for daily. The program should continue getting data sets until the user enters end of file instead of the initial investment. Use simple macros in this program to avoid any "magic numbers" in your code.
#include<stdio.h>
int main()
{
int amount, rate, time, si;
printf(" Enter Principal Amount : ");
scanf("%d", &amount);
printf(" Enter Rate of Interest : ");
scanf("%d", &rate);
printf(" Enter Period of Time : ");
scanf("%d", &time);
si = (amount * rate * time) / 100;
printf(" Simple Interest : %d ", si);
return 0;
}
15. Write a program that prints the accumulated value of an initial investment invested at a specified annual interest and compounded annually for a specified number of years. Annual compounding means that the entire annual interest is added at the end of a year to the invested amount. The new accumulated amount then earns interest, and so forth. If the accumulated amount at the start of a year is acc-amount, then at the end of one year the accumulated amount is: accamount = accamount + accamount * annualinterest ---- Use a function that returns the accumulated value given the amount, interest, and years The prototype is: float calc_acc_amt (float acc_amount, float annual_interest, int years); 16. Modify the function in Problem 15 so that the interest may be compounded annually, monthly, or daily. Assume 365 days in the year. Hint: Use an argument to specify annual, monthly, or daily compounding of interest. If interest is not to be compounded annually the annual interest must be converted to monthly (i.e., interest 12) or daily interest (i.e., interest 365). The interest must then be compounded each year, each month, or each day as the case may beExplanation / Answer
#include<stdio.h>
float calc_acc_amt(float acc_amount,float annual_interest,int freq,int years){
int i,time=years;
float interest=annual_interest;
//based on frequency entered change interest rate and time frame.
if(freq==2){
interest/=12;
time*=12;
}
else if(freq==3){
interest /= 365;
time *= 365;
}
//for each time add interest amount to acc_amount
for(i=0;i<time;i++){
//interest will be in percentage so divide by 100
acc_amount = acc_amount + acc_amount*interest/100;
}
return acc_amount;
}
int main()
{
int amount, rate, years,freq;
while(1){
printf(" Enter Principal Amount : ");
scanf("%d", &amount);
//break if principal amount is invalid
if(amount<0){
break;
}
printf(" Enter Rate of Interest : ");
scanf("%d", &rate);
printf(" Enter number of years : ");
scanf("%d", &years);
printf(" Annually -1 Monthly 2 Daily 3 Enter choice:");
scanf("%d",&freq);
printf(" Final amount : %.2f ", calc_acc_amt(amount,rate,freq,years));
}
return 0;
}
/* sample output
Enter Principal Amount : 100
Enter Rate of Interest : 5
Enter number of years : 5
Annually -1
Monthly 2
Daily 3
Enter choice: 2
Final amount : 128.34
Enter Principal Amount : -1
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.