Tanner Oelke Tanner Oelke - 4 years ago 215
C Question

Calculating APY formula in C

Im trying to calculate an APY formula and when compiled it shoots out 0.0000 when there is an input. I don't know if there is an error in my code or in the way im compiled it, but the answer should be output to .0618 when .06 in input.

/* "APY.c", APY Calculator
Name: Tanner Oelke
Date: 2015/09/01

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

int main(void) {
//define variables
double apy, nominalIntRate;

//ask user for interest rate
printf("Please enter nominal interest rate:");
scanf("%lf", &nominalIntRate);

//calculates apy
apy = exp(nominalIntRate) -1;

printf("The APY is: %.4lf\n", &nominalIntRate);

return 0;


Answer Source

You have a good framework but there are two flaws in your program. First of all, scanf takes a pointer to the variables it reads into so as to be able to alter their values, but printf does not require a pointer, just the value. Secondly, you should print the value you want, not some other value. The line

printf("The APY is: %.4lf\n", &nominalIntRate);

should read

printf("The APY is: %.4lf\n", apy);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download