// CS240 Fall 2009 // Lab 02 Solution // JC Chin #include // Get an integer from STDIN. Inputs are separated by \n and EOF. // Return: The integer read from STDIN. int getInteger() { char temp; int total = 0; while(((temp = getchar()) != '\n') && (temp != EOF)) { if (temp < '0' || temp > '9') return -1; total = total * 10 + temp - '0'; } return total; } int main() { int i; int temp; double rate; double deposit; double amount[12]; // Inputs from the user printf("Amount of money to deposit every month: "); deposit = getInteger(); if(deposit < 0) // make sure the deposit amount is positive. return 0; printf("Annual interest rate (%%): "); rate = getInteger(); if(rate < 0) // make sure the interest rate is positive. return 0; // Compute the monthly interest rate. // rate / 100.0 because the input is percentage. rate = rate / (100.0 * 12.0) + 1.0; // Compute the balance amount[0] = deposit * rate; for(i = 1; i < 12; i++) amount[i] = (amount[i-1] + deposit) * rate; // Print out the balance printf("\n"); printf("Month Balance\n"); printf("==================\n"); for (i = 11; i >= 0; i--) printf("%2d %15.2lf\n", i+1, amount[i]); }