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

คำนวณดอกเบี้ยทบต้น (Compound Interest) ด้วยสูตรใน C


โจทย์

คำนวณจำนวนเงินหลังจากฝากเงินพร้อมดอกเบี้ยทบต้นเป็นปี

สูตร

M = (r/100) * t
A = P * e^M

P = เงินต้น (Principal)
r = อัตราดอกเบี้ย (%)
t = ระยะเวลา (ปี)
A = จำนวนเงินสุทธิ

อัลกอริทึม

  1. ประกาศตัวแปร double
  2. รับ P, r, t
  3. คำนวณ M = (r/100) * t
  4. คำนวณ A = P * exp(M) // ต้อง include
  5. แสดงผล

โปรแกรมตัวอย่าง

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

int main() {
    double P, r, t, A, M;
    
    printf("Principal amount: ");
    scanf("%lf", &P);
    printf("Interest rate (%%): ");
    scanf("%lf", &r);
    printf("Years: ");
    scanf("%lf", &t);
    
    M = (r / 100.0) * t;
    A = P * exp(M);
    
    printf("Amount after %.1f years = %.2f\n", t, A);
    return 0;
}

ผลลัพธ์

amount to be deposit in the bank: 50000
enter the rate of interest: 6.5
How many years you want to deposit: 5
amount after 5.0 years with interest is: 69201.53