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

คำนวณยอดคงเหลือผ่อนชำระ (Balance Installment) ด้วย C

โจทย์

คำนวณยอดคงเหลือที่ต้องผ่อนชำระทุกเดือนสำหรับเงินกู้ (รวมดอกเบี้ย)

สูตรคำนวณ

ดอกเบี้ยต่อเดือน = loanamt * ((interest/100)/12);
ยอดรวมดอกเบี้ย = loanamt + ดอกเบี้ย;
ยอดหลังผ่อนเดือนที่ 1 = ยอดรวม - monthlypayment;
ดอกเบี้ยเดือนที่ 2 = ยอดหลังเดือน1 * ((interest/100)/12);
ยอดหลังผ่อนเดือนที่ 2 = (ยอดหลังเดือน1 + ดอกเบี้ยเดือน2) - monthlypayment;

โปรแกรม

#include <stdio.h>
int main() {
    float loanamt, interest, monthlypayment;
    float i, firstmon, secondmon;
    printf("enter the loan amount: ");
    scanf("%f", &loanamt);
    printf("interest rate: ");
    scanf("%f", &interest);
    printf("monthly payment: ");
    scanf("%f", &monthlypayment);
    i = loanamt * ((interest/100)/12);
    i = i + loanamt;
    firstmon = i - monthlypayment;
    i = firstmon * ((interest/100)/12);
    i = i + firstmon;
    secondmon = i - monthlypayment;
    printf("Remaining after 1st installment: %.2f\n", firstmon);
    printf("Remaining after 2nd installment: %.2f\n", secondmon);
    return 0;
}

ผลลัพธ์

enter the loan amount: 45000
interest rate: 7
monthly payment: 1000
Remaining after 1st installment: 44262.50
Remaining after 2nd installment: 43520.70