I noticed a space before %lf as commented below. Removing this allowed the program to run.
I added the line: i1/=100.0; which gave a correct outcome to the compounded values. I'm not an investments guy and I can't comment on the formula further down the program.
I cast part of the formula to ensure a "double" outcome from the calculation where the denominator was an "int". I may be wrong, but I think makes certain of accuracy. I think its possible for the decimal part to be truncated. Perhaps someone will post a view on this, please?!
Code:
#include <stdio.h>
#include <math.h>
int main() // I made it int main()... : )
{
int t1, t2, t3, n1, n2, n3;
double p1, p2, p3, a1, a2, a3, i1, i2, i3;
/* Decided to initialize all variables to zero... */
t1=t2=t3=n1=n2=n3=0;
p1=p2=p3=a1=a2=a3=i1=i2=i3=0.0;
printf(" \n Investment Calculator ");
printf(" \n --------------------- ");
printf(" \n ");
printf(" \n Entering data for Investment A: \n");
printf(" \n Please Enter the Principal : ");
scanf("%lf", &p1); // There was a space in front of the %lf...
/* testing */
printf(" \n entered : %.2lf", p1); //made it %.2lf...
printf(" \n Please Enter the Annual Interest Rate :");
scanf("%lf", &i1);
i1/=100.0; // This line gives correct final amount... is this a "formula thing"...
// See the results with and without this line...
printf(" \n Please Enter the Number of Compouding Periods per Year: ");
scanf("%d", &n1);
printf(" \n Please Enter the Number of Years over which you are calculating this Investment : ");
scanf("%d", &t1);
a1 = p1 * pow(1.0 + ((double)i1 / n1), (n1 * t1));
// i1 is double, n1 is int: I cast as double to avoid "integer division" where
// accuracy of the decimal in the result can be lost...
printf(" \n The final amount is : %.2lf", a1); // also changed to %.2lf here...
return 0;
}
MisterBadger hopes this is helpful and will gladly receive advice on investments!
Happy Easter weekend!