# Compound interest .... !!!

hi everyone, i need help on that question ....

what is the compound interest formula if i'm using 'i' for the rate and 'n' for the numbers of years ???

i already wrote two functions but they're not working !!!!

//============================================
// First one
//========

public float Total(float i,int n)            //Savings -> return balance after n years and i interest
{
for(int count=0; count<n; count++)
{
Balance *= Math.pow(( 1 + i ), n);
}
return Balance;
}
//================

// Second one
//========

public float Total(float i,int n)            //Savings -> return balance after n years and i interest
{
return Balance *= Math.pow(( 1 + i ), n);
}
//============================
i don't know what one is the correct one .... i'm not a business major :P

and if by any chance ..... could anyone tell me how to do it in the recursive way !!!

thankz
LVL 1
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Out of interest (no pun intended) why would you want to do it recursively?
0
Commented:
I reckon it says to as part b of the homework... ;-)
0
Commented:
Compound Interest is calculated like:

public calculateCompoundInterest( double rate, double period )
{
balance = balance * Math.pow( 1+rate, period ) - balance ;
}

Make sure your "balance" variable is a double...(not an int)

Tim
0
Commented:
Tell your teacher that recursion should be avoided where it's not necessary (as it is not here;-))

The formula for simple interest is:   F  =  P ( 1 + R / N ) ^ ( N * T )

Where :  F   =  final amount
P   =  initial amount
R   = interest rate
N   = number of compounding periods
T   = Time ( in years )

0
Associate Director - Product EngineeringCommented:
More importantly, the recursive function that he's written will go on looping forever as there's no stopping condition.

BTW, hackermen5, this is not a place to tell you the correct formula :-) we can help you with the coding/ implementation if you face problems.

I guess he needs recursion in computing the value of (1 + R/N) ^ T.... (for computing the exponential value). Strange way.

Mayank.
0
Commented:
>  More importantly, the recursive function that he's written will go on looping forever as there's no stopping condition.

Which one do you think is recursive?
0
Commented:
;-)
0
Associate Director - Product EngineeringCommented:
public float Total(float i,int n)            //Savings -> return balance after n years and i interest
{
return Balance *= Math.pow(( 1 + i ), n);
}
//============================

Sorry, I thought I saw a call to Total () instead of pow :-) didn't read it completely.... thought that the first was non-recursive and the second recursive.

Anyways, he's still not told us why he needs the recursion.
0
Commented:
public float Total(float i,int n)
{
float balance=0;
for(int count=0; count<n; count++)
{

balance*= Math.pow(( 1 + i ), n);
}
return balance;
}
0
Associate Director - Product EngineeringCommented:
Dannin, your balance will always remain zero.
0
Associate Director - Product EngineeringCommented:
Also, by the way, why is balance supposed to multiplied by (( 1 + i ) ^ n) 'n' number of times?
0
Commented:
Opps I'm too used to addition lol

public float Total(float i,int n)
{
float balance=1;
for(int count=0; count<n; count++)
{

balance*= Math.pow(( 1 + i ), n);
}
return balance;
}
Here u go
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
Well isn't it the way the person who asked the question wanted it?
0
Java DeveloperCommented:
> Tell your teacher that recursion should be avoided

except if you're learing about recursion :)
0
Commented:
Then an example that needs recursion should be given
0
Java DeveloperCommented:
An (familiar) problem that can be solved with recursion is just as suitable.
0
Author Commented:
i'm sorry uys ... but my teaher is not that nice to give us an example of recursion .... i didn't even understand the recursion concept yet .....

anyway ..... thanks for ur help guys .....
0
Associate Director - Product EngineeringCommented:
By the way, why did you accept my comment as an assist? (I was still waiting to assist after getting some comments from you :-) )
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.