# Thread: trying to use pow()

1. ## trying to use pow()

im trying to use the power function pow() but when I do the program fails to compile
with undefind refer to pow

Code:
```// calculate loan payments on a mortgage

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

int main()

{

// declare all variables

double principal_amount = 0;
double interest_rate = 0;
int years = 0;

system ("clear");

printf ( " This program will calculate your monthly payment of a Home Mortgage\n\n");
printf ( " First it will need some info \n\n" );
scanf  ( " Please enter the loan amount:\t %d \n\n", &principal_amount );
scanf  ( " Next how many years would you like the loan for?\t %d \n\n", &years );
scanf  ( " And last what intrest rate are you getting?\t %d \n\n", &interest_rate );

int number_of_payments = years * 12;
double monthly_interest = interest_rate / 12;
double  x = pow( 1.0 + monthly_interest, number_of_payments );
double monthly_payment = ( principal_amount * x * monthly_interest ) / ( x - 1 );

printf ( "\n\n Your monthly payment would be %d.2\n\n ", monthly_payment );

return 0;

}```

2. Your problem is that you are declaring and initializing variables later in the program. A good, general guide to follow is to declare and initialize all of your variables (if they need to be initialized -- and initialize them with numbers) at the start of your program and then calculate the values for them later.

I did a test run with your program, declaring all your variables at the start and then calculating their values later, and it compiles correctly. Whether it works correctly or not from there, is up to you to test!

P.S. You used pow() correctly.

3. The way you've done things (declaring variables later) is C++, not C. Move declarations to the top of the function to address that.

Apart from that, your problem isn't compilation. It is more likely to be a linker problem. With a lot of development environments, the math library is not linked in by default.

With Unix compilers (eg gcc) this is addressed by adding "-lm" to the command line. With windows development environments, there is usually an option somewhere concerned with using floating point libraries.

An alternative, in your case, is to replace;
Code:
`double  x = pow( 1.0 + monthly_interest, number_of_payments );`
with;
Code:
```double temp = 1.0 + monthly_interest;
double x = 1.0;
for (int i = 0; i < number_of_payments; ++i)
x *= temp;```

4. ## yup it was a linker problem

yea it was a linker problem I figured that out

I also found many other problems with my code so I fixed it

Code:
```// calculate loan payments on a mortgage

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

int main()

{

// declare all variables

double principal_amount = 0;
float interest_rate = 0.0 ;
int years = 0;

system ("clear");

printf ( " This program will calculate your monthly payment of a Home Mortgage\n\n" );
printf ( " First it will need some info \n\n" );
printf ( " Please enter the loan amount: " );
scanf  ( "%d", &principal_amount );
printf ( "\n\n Next how many years would you like the loan for? " );
scanf  ( "%d", &years );
printf ( "\n\nAnd last what intrest rate are you getting? " );
scanf  ( "%3f", &interest_rate );

int number_of_payments = years * 12;
float monthly_interest = interest_rate / 12;
double monthly_payment = principal_amount * pow ( 1.0 + monthly_interest, number_of_payments )  * monthly_interest / ( pow ( 1.0 + monthly_interest, number_of_payments ) - \$

printf ( "\n\n Your monthly payment would be \$%d dollars\n\n ", monthly_payment );
printf ( "\n Principal amount is %d", principal_amount );
printf ( "\n Number of payments is %d", number_of_payments );
printf ( "\n Monthly interest is %f", monthly_interest );
printf ( "\n Interest rate is %3f\n", interest_rate );

return 0;

}```

it runs now

5. In addition, some printf and scanf calls are wrong
foo.c:21: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘double *’
foo.c:30: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘double’

6. For float and double, you need to use %f or %g format specifiers rather than %d (which is for int).

You're lucky; not all compilers check that for you.

7. Originally Posted by grumpy
For float and double, you need to use %f or %g format specifiers rather than %d (which is for int).

You're lucky; not all compilers check that for you.
I looked at what your are saying and I did some reading and some testing.

my school book say that you should express an dollar amount as a "%d" format specifer
cuase your talking about money. But I did see the warnings the other person got.

So how could I correct this. I tried changing both the "%f" but after I recompiled it my
awnser was no-longer correct.

I tested the program with 100,000 dollar @ 7% interest rate over 30 years. it should
come back with a \$665 monthly payment. after making the format change my answer was
wrong.

8. Your example does not compile because of this line (split for readability):
Code:
```double monthly_payment = principal_amount * \
pow ( 1.0 + monthly_interest, number_of_payments )  * \
monthly_interest / ( pow ( 1.0 + monthly_interest, number_of_payments ) - \$```

9. Code:
```double principal_amount = 0;
scanf  ( "%d", &principal_amount );```
%f is for floats, %lf is for doubles, and %d is for integers.

10. Originally Posted by dwks
Your example does not compile because of this line (split for readability):
Code:
```double monthly_payment = principal_amount * \
pow ( 1.0 + monthly_interest, number_of_payments )  * \
monthly_interest / ( pow ( 1.0 + monthly_interest, number_of_payments ) - \$```
The \'s at the end of the first two lines in the above code aren't necessary.