# Thread: algorithm question?

1. ## algorithm question?

I'm writing this program that takes in a dollar amount (a) and then displays the the balance (t) after (n) number of years that pays (r) percent interest, compounded annually. My program isn't displaying the results correctly. Can someone tell me if this is at least right?:

Code:
`t=a*1+pow(r,n);`

2. http://en.wikipedia.org/wiki/Compoun...pound_interest says your formula may be wrong.

Express the problem mathematically, then attempt to convert it to a C expression.

3. Yes, your formula is wrong. It's *almost* right for simple interest, but definitely not compound interest. Compound interest involves the sum of a geometric series. I'll be happy to explain it if, after doing some research, you still don't know how to do it.

4. I found the formula on the net that explains the way to compute compound interest a=p(1+r\n)nt where a = amount after time t,
p = principal amount, r = annual interest rate, n = number of time the interest is compounded per year and t = number of years. Do I need to declare another variable? I'm still a little confused because my program below displays the following result:

"Your balance after 2289404 years with an interest rate of Inf would be \$-0.000000."

...if I enter a dollar amount deposited of 1500.00 with .07 interest and 20 years for the input.

Code:
```/*
Filename:    balance.c

Description: Displays the balance with interest compounnded annually,
after number of years entered.

*/

# include <stdio.h>
# include <math.h>

void introduction (void);

double ComputeBalance (double a, double r, int n, double t);

int main()

{

/*Allocate memory for data*/

introduction ();

double a,r,t;

int n;

/*Prompt user for data*/

printf ("\nEnter the the dollar amount deposited: \$");
scanf ("%lf",&a);

printf ("\nEnter the interest rate:");
scanf ("%lf",&r);

printf ("\nEnter the number of years:");
scanf ("%d",&n);

/*Processing and display results*/

printf ("\nYour balance after %d years with an\n");

printf ("interest rate of %lf would be"
" \$%lf\n",ComputeBalance(n,r,t,a));

return 0;

}

void introduction (void)

{

printf ("\nThis program displays the balance with interest,\n");
printf ("compounded annually after number of years entered.");

}

double ComputeBalance(double a,double r, int n, double t)

{

return t=a*1+pow(r,n);

}```

5. Well, one problem (not the only one) is that you call it as:
Code:
`ComputeBalance(n,r,t,a)`
Yet the function has the parameters in this order:
Code:
`double ComputeBalance(double a,double r, int n, double t)`
So the n in main is passed to the a in ComputeBalance, r to r, t to n, and a to t. Is this what you want to do?

6. OK...I changed

Code:
`ComputeBalance(n,r,t,a)`
to...

Code:
`ComputeBalance(a,r,n,t)`
now I get...

"Your balance after 2289404 years with an interest rate of Inf would be \$0.000000."

...if I enter a dollar amount deposited of 1500.00 with .07 interest and 20 years for the input.

7. Code:
`printf ("\nYour balance after %d years with an\n");`
You supply a %d to the format argument, but you're not passing anything along with it! You _must_ pass a integer with that specifier.

Code:
```printf ("interest rate of %lf would be"
" \$%lf\n",ComputeBalance(n,r,t,a));```
That printf() statement needs work as well, and your compiler should not let you compile the program.

Your ComputeBalance() function is wrong. First, this line:
Code:
`return t=a*1+pow(r,n);`
Why assign the answer to t? You never use t again. The "t=" has no effect. Also, as others in this thread have said, your formula for compounded interest is wrong. I suggest you read the wiki article that cwr posted.

8. Wouldn't my scanf for interest that is inputted pass to the format arguement?

Code:
```printf ("\nEnter the number of years:");
scanf ("%d",&n);```
I changed my format argument to this:

Code:
```    printf ("\nYour balance after %d years with an\n");

printf ("interest rate of %lf would be"
" \$%lf\n",n,r,ComputeBalance(a,r));```
Doesn't the scanf prior to my output statement pass the n variable? I removed the t variable from my program but I'm still struggling with the compounded interest. I'm sorry...I'm such a rookie when it comes to C.

9. You're very confused. You need to read up on formatted input/output in your C book.

As Cactus_Hugger said:
Code:
`printf ("\nYour balance after %d years with an\n");`
is wrong. That says: output "\nYour balance after %d years with an\n" but replace %d with an integer passed to the function. printf looks for the integer, can't find it, and prints garbage. You need:
Code:
`printf("\nYour balance after %d years with an\n", n);`
Further, the second line:
Code:
```printf ("interest rate of %lf would be"
" \$%lf\n",n,r,ComputeBalance(a,r));```
Is also wrong. You have two format specifiers (%lf twice) but three parameters. Programming is a very precise thing. You cannot just keep trying stuff until it works, you need to understand what you're doing.

As for the compound interest formula, it seems to be the least of your problems, but once your code is correct (the printf format specifiers are correct and match up appropriately), tell us what you think the formula for compound interest is, given your variables, and then what C expression you think calculates that formula, and we'll help you if there are any errors.

Popular pages Recent additions