0

//A program that calculates the amount of money in a bank account after n years

#include <stdio.h>

double bank(double money, double apy, int years);

int main() {

double money1, apy1;
int years1;

printf("How much money is currently in your bank account? ");
scanf("%d", &money1);

printf("How many years will this money stay in your account? ");
scanf("%d",&years1); 

printf("What is your APY? ");
scanf("%d", &apy1); 

int bank1 = bank(money1, apy1, years1);

printf("Your grand total after %d will be $%d \n", years1, bank1); 


system ("PAUSE");
return 0;   
}


double bank(double money, double apy, int years) {

 if(years <= 0) 
    return money;

 else
    return bank(money*apy, apy, years-1);

 }
7
  • 6
    Seriously? "What is wrong with this code?" is your entire "question"? Commented Jan 24, 2011 at 21:54
  • 3
    Is it not compiling? Is it failing in some other way? I'm afraid you'll have to be more specific. Commented Jan 24, 2011 at 21:54
  • Its not calculating the total correctly. Commented Jan 24, 2011 at 21:57
  • 1
    @Krysten you might want to edit your question to ask exactly why its not calculating the total correctly. Commented Jan 24, 2011 at 21:58
  • 2
    www.homeworkoverflow.com Commented Jan 24, 2011 at 22:06

5 Answers 5

4

Change:

scanf("%d", &money1);

to

scanf("%lf", &money1);

and change:

scanf("%d", &apy1); 

to:

scanf("%lf", &apy1); 

And while you're at it you might want to add some printfs to help with debugging (assuming you don't have a source level debugger.)

Sign up to request clarification or add additional context in comments.

Comments

4

This:

return bank(money*apy, apy, years-1);

should probably be

return bank(money*(1+apy), apy, years-1);

since the interest you earn should be added to the existing amount. Otherwise your total amount would be reducing each year.

2 Comments

Also the return from bank is being cast to int but I'm not sure if this is deliberate or not.
that doesn't seem to give me the desire output
2

Another one is :

double bank(double money, double apy, int years);

Returns a double, but

int bank1 = bank(money1, apy1, years1);

You place the result in an int.

Comments

1

You should never use floating point in financial calculations.

Floating point is inherently uncapable of representing 10-base values precisely, which means that you will suffer from rounding errors and unequalities, which is unacceptable in finances (among others).

This has been discussed in detail many times on SO. The issue is not language specific.

Comments

0

I think you should call you function in the following way:

int bank1 = bank(money1, 1+apy1/100., years1);

Otherwise you'll have a LOT of money :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.