love

วันพุธที่ 16 กันยายน พ.ศ. 2558

Lab4 - Loan Payment

void setup(){
  loan_money(5000,0.12,1);
}

//function for calculate and output
void loan_money(float amount, float rate, int year){
  int month = year*12; //cal month
  float month_rate = rate/12;  //calculate month rate
  float month_paid = amount*(month_rate/(1-pow(1+month_rate,-month)));  //paid per month
  float principal = month_paid;  //paid per month
  float balance = amount;       //Unpaid
  float interest_total = 0;     // start interest total
  int count = 1;    //month start value
  println("Num.  Interest  Principal  Unpaid  Interest total");
 
  //loop for calculation
  while(count <= month){
    print(nf(count, 2)); //Num
    print("     "+nf(month_rate*balance, 2, 2)); //Interest
    interest_total+=month_rate*balance;
    principal=month_paid-(month_rate*balance);
    balance -= principal;
    if(balance < 0){
      balance = 0;
    }
    print("     "+nf(principal, 3, 2)); //Principal
    print("    "+nf(balance, 4, 2)); //balance
    print("     "+nf(interest_total, 3, 2)); //Total
    println();
    count++;
  }
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น