Untitled

Anonymous
plain_text
02/18/2026 4:25 PM
1.6 KB
13
Indexable
#include <iostream>
using namespace std;

int main() {
    double money;
    double rate_percent;
    int years;

    cout << "=== Compound Interest Calculator ===\n";
    
    // 1. รับค่าเงื่อนไขเริ่มต้น (Base Case)
    cout << "Enter initial money (THB): ";
    cin >> money;
    
    cout << "Enter interest rate per year (%): ";
    cin >> rate_percent;

    cout << "Enter years to save: ";
    cin >> years;

    // แปลง % เป็นทศนิยม เช่น 5% -> 0.05
    double rate = rate_percent / 100.0;

    cout << "\n[Recurrence Relation: Year(n) = Year(n-1) + Interest]\n";
    cout << "Year 0: " << money << " THB (Base Case)\n";

    // 2. จำลองความสัมพันธ์เวียนเกิดด้วย Loop
    for (int i = 1; i <= years; i++) {
        // เอาเงินปีที่แล้ว (money เดิม) มาบวกดอกเบี้ย แล้วเซฟทับเป็นเงินปีนี้
        money = money + (money * rate); 
        
        cout << "Year " << i << ": " << money << " THB\n";
    }

    return 0;
}
Editor is loading...
Leave a Comment