Ryan Glenn
Ryan Glenn

Reputation: 1416

Calculating Interest Rate

So I am trying to learn C++ for a college course and I have to write a program which uses this formula:

Amount = Principal * (1 + Rate/T)^T

Where principal is the balance in savings, rate is the interest rate, and t is the number of times the interest is compounded during a year. According to the book if you type in 4.25 as the interest rate and 12 as the number of times compounded with the principal as 1000.00 then you should get 43.34 as interest and the total amount should be 1043.34. I'm not sure if I am coding it wrong or what but I was wondering if anyone could help me out with any mistakes I might have done! I'm trying to do it on my own for about a day or two now but I have had no luck.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double PRINCIPAL;
    double INTEREST_RATE;
    double COMPOUND_AMOUNT;


    cout << "What is your savings account balance?: " << endl;
    cin >> PRINCIPAL;
    cout << "What is your annual interest rate?: " << endl;
    cin >> INTEREST_RATE;
    cout << "How many times has your interest been compounded?: " << endl;
    cin >> COMPOUND_AMOUNT;

    double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
    double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);

    cout << "Interest Rate: " << INTEREST_RATE << endl;
    cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
    cout << "Principal: " << PRINCIPAL << endl;
    cout << "Interest: " << INTEREST_RATE * COMPOUND_AMOUNT << endl;
    cout << "Amount: " << AMOUNT << endl;

    system("pause");
    return 0;
}

Upvotes: 0

Views: 18759

Answers (1)

Nicolas Holthaus
Nicolas Holthaus

Reputation: 8293

This is a math error. If you're going to take in interest rates as '4.25' %, you need to divide the interest rate by 100. The code below gave me the amount as 1043.34 when 4.25 is entered as the interest rate.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int main()
{
    double PRINCIPAL;
    double INTEREST_RATE;
    double COMPOUND_AMOUNT;


    cout << "What is your savings account balance?: " << endl;
    cin >> PRINCIPAL;
    cout << "What is your annual interest rate? (in %): " << endl;
    cin >> INTEREST_RATE;
    INTEREST_RATE /= 100;
    cout << "How many times has your interest been compounded?: " << endl;
    cin >> COMPOUND_AMOUNT;

    double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
    double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);

    cout << "Interest Rate (%): " << INTEREST_RATE * 100 << endl;
    cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
    cout << "Principal ($): " << PRINCIPAL << endl;
    cout << "Interest ($): " << AMOUNT - PRINCIPAL << endl;
    cout << "Amount ($): " << AMOUNT << endl;

    system("pause");
    return 0;
}

for interest your book is talking about the amount of interest in dollars, i.e. AMOUNT - PRINCIPAL.

Upvotes: 1

Related Questions