Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรมคำนวณค่างวดผ่อนชำระรายเดือน (EMI) ในภาษา C

EMI (Equated Monthly Installment) คืออะไร?

EMI ย่อมาจาก Equated Monthly Installment คือ จำนวนเงินผ่อนชำระรายเดือนที่เท่ากันทุกงวด โปรแกรมนี้จะคำนวณค่าผ่อนชำระรายเดือนจากเงินต้น อัตราดอกเบี้ย และระยะเวลา

สูตรการคำนวณ EMI

EMI = (P × R × (1+R)ᵀ) / ((1+R)ᵀ - 1)

โดยที่:

  • P = เงินต้น (Principal amount)
  • R = อัตราดอกเบี้ยต่อเดือน (รายปี / 12 / 100)
  • T = ระยะเวลาผ่อนชำระเป็นเดือน (ปี × 12)

ตัวอย่าง

Input: principal = 2000, rate = 5%, time = 4 ปี
Output: Monthly EMI is = 46.058037

อัลกอริทึม

  1. รับค่า principal, rate, time
  2. แปลงอัตราดอกเบี้ยเป็นรายเดือน: r = rate / (12 * 100)
  3. แปลงระยะเวลาเป็นเดือน: t = time * 12
  4. คำนวณ EMI ตามสูตร
  5. แสดงผล

โค้ดภาษา C

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

float calculate_EMI(float p, float r, float t) {
    float emi;
    r = r / (12 * 100);  // อัตราดอกเบี้ยต่อเดือน
    t = t * 12;          // ระยะเวลาเป็นเดือน
    emi = (p * r * pow(1 + r, t)) / (pow(1 + r, t) - 1);
    return emi;
}

int main() {
    float principal = 2000, rate = 5, time = 4, emi;
    emi = calculate_EMI(principal, rate, time);
    printf("\nMonthly EMI is= %f\n", emi);
    return 0;
}

ผลลัพธ์

Monthly EMI is= 46.058037