C Tutorial
This c program is used to compute the equated monthly installment and round up the computed EMI value using match floor function.
C predefined function returns the largest integer value less than or equal to parameter value.
double floor(double)
if we are invoking floor function for parameter 17.8 like floor(17.8) then returns 17.00000.
for floor(17.2) also returns 17.00000 and it's not actual round up value.
Let's see how we can round up the emi decimal value using floor function in below program,
#include<stdio.h> #include<math.h> float PMT (int interest, int loan_amount, int total_months) { float rate, denominator; rate = (float) interest/(100*12); denominator = pow((1+rate), total_months)-1; return (rate+(rate/denominator))*loan_amount; } void main() { int loan_amount, loan_term_years, num_of_emi_per_year; float rate_of_interest, pmt_value, pmt_floor_val; printf("\nEnter loan amount: "); scanf("%d", &loan_amount); printf("\nEnter loan tearm in years: "); scanf("%d", &loan_term_years); printf("\nEnter number of EMI per year: "); scanf("%d", &num_of_emi_per_year); printf("\nEnter rate of interest: "); scanf("%f", &rate_of_interest); pmt_value= PMT(rate_of_interest, loan_amount, loan_term_years*num_of_emi_per_year); pmt_floor_val = floor(pmt_value); // round up logic if(pmt_value >= (pmt_floor_val+0.5)) { pmt_value = pmt_floor_val +1; } else { pmt_value = pmt_floor_val; } printf("\nPMT: %f", pmt_value); }
Output:
$ cc emi_calculator.c -lm $ ./a.out Enter loan amount: 396000 Enter loan tearm in years: 4 Enter number of EMI per year: 12 Enter rate of interest: 15 EMI: 11021.00000
C Tutorial
Privacy Policy | Copyright2020 - All Rights Reserved. | Contact us
| Report website issues in Github
| Facebook page
| Google+ page