# Solved: Help with calculating interest

Discussion in 'Software Development' started by SuitedMonkey, Feb 19, 2013.

Not open for further replies.

Joined:
Nov 10, 2012
Messages:
51
ok so the basics of the code is that the user enters the Principal value, the interest rate, and the number of years to let it compound. Normally pretty easy but there's a catch; I can't use the math library.

So i did what i think is the code but there are a ton of errors that i don't understand. Any help would be appreciated!
Code:
```#include <stdio.h>
double calculation(double prin_val, int time, double rate)
void main() {
double future_val, principle_val, double int_rate; //future value, principle value, interest rate
int time;

printf("Please enter the principal value and interest rate followed by the number of periods.\n");
scanf("%lf, %lf, %d", &principle_val, &time);

calculation(principle_val, time, int_rate);

printf("The future value is: %.2lf \n", future_val);
}
double calculation(double principle_val,int time, double int_rate){
double fut_val;
int_rate +=1;
do {
int_rate =* int_rate;//alternative for exponents
time--;
}while(time<0);

fut_val = principle *int_rate;
return fut_val;
}
```

2. ### andythepandy

Joined:
Jul 2, 2006
Messages:
379
There are a few basic errors in the code like operators being the wrong way around (=* instead of *=) and variables names being typed incorrectly. Below is an edited version with comments where I changed parts. This code compiles but it doesn't look like it gives the correct output, hopefully it will get you started though:

Code:
```#include <stdio.h>
//ADDED SEMI COLON AT END OF NEXT LINE
double calculation(double prin_val, int time, double rate);
int main() {
//REMOVED SECOND DOUBLE ON NEXT LINE, UNNECESARY
double future_val, principle_val, int_rate; //future value, principle value, interest rate
int time;

printf("Please enter the principal value and interest rate followed by the number of periods.\n");
scanf("%lf, %lf, %d", &principle_val, &time);

calculation(principle_val, time, int_rate);

printf("The future value is: %.2lf \n", future_val);
return 0;
}
double calculation(double principle_val,int time, double int_rate){
double fut_val;
int_rate +=1;
do {
//CHANGED TO *= here
int_rate *= int_rate;//alternative for exponents
time--;
}while(time<0);

//CHANGED PRINCIPLE TO PRINCIPLE_VAL AS THAT IS WHAT THE VARIABLE IS CALLED
fut_val = principle_val *int_rate;
return fut_val;
}

```

Joined:
Nov 10, 2012
Messages:
51
ok that worked. but it didn't work. i'm trying to use the math library now so that i can use the pow() function. here's the code
Code:
```double calculation(double principle_val,int time, double int_rate){
double fut_val;//futute value
double rand_val; //just a holding place
int_rate +=1;
rand_val = pow(int_rate,time);
fut_val = principle_val * rand_val;

return fut_val;
}
```
and here's the error i get
Code:
```(. text+0x105) : undefined reference to 'pow'
collect2: ls returned 1 exit status
```
any help would be apreciated

4. ### andythepandy

Joined:
Jul 2, 2006
Messages:
379
Have you included the math library. I think some compilers include it automatically but don't know if they all do

Joined:
Nov 10, 2012
Messages:
51
yea it's #include<math.h>
i'm using putty

Joined:
Jul 2, 2006
Messages:
379

As Seen On