C Tutorial

# C program to compute EMI and round up using ceil

## C program to compute EMI and round up the decimal value using ceil function

This c program is used to compute the equated monthly installment and round up the computed EMI value using match ceil function.

## c ceil function

C predefined function returns the smallest integer value greater than or equal to parameter value.

double ceil(double)

if we are invoking ceil function for parameter 17.8 like ceil(17.8) then returns 18.00000.

for ceil(17.2) also returns 18.00000 and it's not actual round up value.

Let's see how we can round up the emi decimal value using ceil 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_ceil_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_ceil_val = ceil(pmt_value);
if(pmt_value >= (pmt_ceil_val-0.5))
{
pmt_value = pmt_ceil_val;
}
else
{
pmt_value = pmt_ceil_val-1;
}
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