Loan calculator in C

I wanted to create a loan calculator, but the code I could think of was using 3 functions, and calling them and getting result. But I felt it was easy but bad code because I thought this can be written in fewer lines and with more quality. I wanted to ask what are some things to keep in my mind while trying to write better code instead of going in flow and creating many functions.

#include #include #include float loanAmount,interestRate,Discount,i,loanPayment; //global variables int years; void loanDetails(void); //input from user regarding the loan float annualRate(void);// interest rate float discount(void); //discount to be added int main() //main < loanDetails(); //called three functions annualRate(); discount(); loanPayment = (loanAmount)/(Discount); //formula for monthly payments printf("\nthe monthly loan payment is %f",loanPayment); return EXIT_SUCCESS; >void loanDetails(void) //taking input from user < printf("please enter Total Loan Amount: "); scanf("%f",&loanAmount); printf("please enter the number of years: "); scanf("%d",&years); printf("please enter the annualrate to be applied: "); scanf("%f",&interestRate); >float annualRate(void) //calculated annual rate < float *ann; ann = &interestRate; i = ((*ann)/(100))/12; printf("the value for %d years is %f",years,i); return i; >float discount(void) //calculated dicount < float *x,y; x = &i; y = 1 + *x; int n = years*12; float topD = (pow((y),n)-1); float botD = (y-1)*pow((y),n); Discount = topD/botD; printf("\nthe value of discount is : %f",Discount); return Discount; >
144k 22 22 gold badges 188 188 silver badges 472 472 bronze badges asked Feb 23, 2019 at 5:13 23 1 1 silver badge 4 4 bronze badges

\$\begingroup\$ Welcome to Code Review! While I think the main charm of the traditional C indent of 8 positions is discouraging heavily nested structures, please indent your code consistently. \$\endgroup\$

Commented Feb 23, 2019 at 6:28

\$\begingroup\$ Some things are unclear. For example, when calculating the annual interest rate, you do i = ((*ann)/(100))/12; , which implies you're calculating a monthly rate because you're dividing by 12. You're also relying too much on global variables instead of utilizing function parameters. \$\endgroup\$

Commented Feb 24, 2019 at 1:46

2 Answers 2

\$\begingroup\$

There are a few things to note here. There is extensive referencing and dereferencing of variable addresses, which makes the code less clear. For example, float *x,y; x = &i; y = 1 + *x; . This is unnecessary and seems imposed by thinking related to your use of global variables.

There is nothing wrong with passing pointers as arguments to functions, rather than using global variables.

There is also nothing wrong with using functions. The code below is clearer. Additionally, it removes the need for the function "annualRate," as that is simply a division.

#include #include #include void loanDetails(float * loanAmount, int * years, float* interestRate); float discount(int years, float i); int main (void) < float loanAmount, interestRate, disc; int years; loanDetails(&loanAmount, &years, &interestRate); disc = discount(years, interestRate/1200); printf("\nthe value of discount is : %f",disc); printf("\nthe monthly loan payment is %f",loanAmount/disc); return EXIT_SUCCESS; >void loanDetails(float * loanAmount, int * years, float* interestRate) < printf("please enter Total Loan Amount: "); scanf("%f",loanAmount); printf("please enter the number of years: "); scanf("%d",years); printf("please enter the annualrate to be applied: "); scanf("%f",interestRate); >float discount(int years, float i) < int months = years*12; return (pow((1+i),months)-1)/((i)*pow((1+i),months)); >

If you wanted no functions other than main, you could do the following. Essentially, move the code from the functions to main. This is appropriste, considering that the code is not repeated, runs sequentially, and does not have varied/multiple inputs.

#include #include #include int main (void) < float loanAmount, interestRate, disc, i; int years, months; printf("please enter Total Loan Amount: "); scanf("%f",&loanAmount); printf("please enter the number of years: "); scanf("%d",&years); printf("please enter the annualrate to be applied: "); scanf("%f",&interestRate); months = years*12; i = interestRate/1200; disc = (pow((1+i),months)-1)/((i)*pow((1+i),months)); printf("\nthe value of discount is : %f",disc); printf("\nthe monthly loan payment is %f",loanAmount/disc); return EXIT_SUCCESS; >