The problem is: Write a program (in C) that prints the accumulated value of an i
ID: 3887333 • Letter: T
Question
The problem is:
Write a program (in C) that prints the accumulated value of an initial investment invested at a specified annual interest and compounded annually for a specified number of years. If the accumulated amount at the start of a year is acc_amount, then at the end of one year the accumulated amount is:
acc_amount = acc_amount + acc_amount * annual_interest
Use a function that returns the accumulated value given the amount, interest (as a decimal, so 5% = 0.05), and years. The prototype is:
float calc_acc_amt(float acc_amount, float annual_interest, int years);
My professor also gives us some more information: the program should process user input until the user enters 0 for the initial amount.
This is my code so far-- right now i'm having trouble with the while loop (the loop works, but when i put in 0 for initial amount it doesn't quit & only quits after i put in zero as all my inputs). I also am not sure how to go about doing more than 1 year of compounding.
Thank you!
int main() // variables float acc_amount; float annual_interest; int years; float total II initial amount // annual interest rate as decimal II amount of years being compounded // total anount // prompt user for initial amount printf("Enter initial investment (0 to quit): "); scanf("%f", &acc-amount;); // prompt user for annual interest rate printf("Enter annual interest rate:"; scanf("%f", &annual-interest;); /I prompt user for amount of years printf("Enter number of years being compounded: "); scanf("%d", &years;); while (accamount 0) II user Loop - II compute anount, call calc_acc_amt fxn total calc_acc_amt (acc_amount, annual_interest, years); printf("Enter initial investment (0 to quit): "); scanf("%f", &acc-amount; ) ; printf("Enter annual interest rate:"; scanf("%f", &annual-interest;); printf("Enter number of years being compounded: "); scanf("%d", &years;); /I end program printf("Program ended.n") Ii calc acc amt function float calc_acc_amt(float acc_amount, float annual_interest, int years) /I variables float total; /I compute total account amount totalacc_amount acc_amount annual_interest; // total account amount // print result printf("The total account amount is %4.2f ", total); return total; 64,0-1 BotExplanation / Answer
#include<stdio.h>
float calc_acc_amt(float acc_amount, float annual_interest, int years){
int i;
float sum;
sum = acc_amount;
for(i=0; i<years; i++){
sum = sum + (annual_interest/100) * acc_amount;
}
return sum;
}
int main(){
float acc_amt;
float intr_rate;
int years;
float total;
float sum;
printf("Enter initial amount:");
scanf("%f",&acc_amt);
printf("Enter rate of intrest:");
scanf("%f",&intr_rate);
printf("Enter number of years:");
scanf("%d",&years);
sum = calc_acc_amt(acc_amt,intr_rate,years);
printf("Accumulated amount : %f", sum);
while (1){
printf(" Enter initial amount:");
scanf("%f",&acc_amt);
if (acc_amt <= 0)
break;
printf(" Enter rate of intrest:");
scanf("%f",&intr_rate);
printf(" Enter number of years:");
scanf("%d",&years);
sum = calc_acc_amt(acc_amt,intr_rate,years);
printf("Accumulated amount : %f", sum);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.