0

Hi could someone please help me with my code? I want to calculate an accumulate investment after a given amount of years, months, or days. I am reading in an annual interest from the user and would like to have this converted into monthly and daily interest based on the user's chosen type of compounding. I am using an if statement to do this. However, when I run the program, why do I received 3 sets of values for the 3 different types of compounding? I want to receive only 1 set, but I don't know what am I doing wrong?

```
/* File: account.c
* * Date: February 2, 2010 */
/* This program calculates the accumulated value of a given initial investment and annual interest, given the options of either annual, monthly, or daily
* compounding */
#include <stdio.h>
/* Function declaration */
float calc_acc_amt(int x, float acc_amount, float annual_interest, int y);
main()
{
/* Variable declarations */
float initial; /* Input initial value of investment */
float interest; /* Input annual interest */
float input; /* User input of initial value and annual interest */
int type; /* Input type of compounding */
int start = 1; /* Used for start of loop in function */
float acc_amt; /* Accumulated amount of investment invoked by function */
float monthly_interest; /* Modified interest due to chosen input of monthly compounding */
float daily_interest; /* Modified interest due to chosen input of daily compounding */
int length; /* Length of compounding */
/* Program intro */
printf("This program will calculate the accumulated value of your investment at an annual, monthly, or daily compounding\n");
/* Obtain input from user */
printf("Please enter the following data, seperated by spaces.\n");
printf("Initial value, annual interest: ");
input = scanf("%f %f", &initial, &interest);
printf("Type of compounding: (1) annually, (2) monthly, (3) daily: ");
input = scanf("%d", &type);
printf("Length of compounding: ");
scanf("%d", &length);
while (input != EOF) /* Initialize loop */
{ if (type = 1)
{ /* Invoke function */
acc_amt = calc_acc_amt(start, initial, interest, length);
}
if (type = 2)
{
monthly_interest = (interest)/(12);
acc_amt = calc_acc_amt(start, initial, monthly_interest, length);
}
if (type = 3)
{
daily_interest = (interest)/(365);
acc_amt = calc_acc_amt(start, initial, daily_interest, length);
}
if (type != 1 || type != 2 || type != 3)
{
printf("Please indicate type of compound.");
break;
}
/* Update loop */
printf("Initial value, annual interest: ");
input = scanf("%f %f", &initial, &interest);
printf("Type of compounding: (1) annually, (2) monthly, (3) daily: ");
input = scanf("%d", &type);
}
} /* End main */
/* Function */
float calc_acc_amt(int x, float acc_amount, float annual_interest, int y)
{float value;
while (x <= y)
{ printf("%d: ", x);
if (x != 1) /* when x is not equal to 1 */
{
value = value + (value * annual_interest);
printf("Total accumulated value is $%f\n", value); }
else /* when x = 1 */
value = (acc_amount + (acc_amount * annual_interest));
{ printf("Total accumulated value is $%f\n", value); }
x = x + 1;
}
}
```

*Edited 6 Years Ago by tquiva*: n/a