So I have this so far, not sure how to make dollars * e^(rate*time)

#include <iostream> 
#include <cmath> 
#include <iomanip> 
using namespace std;

int main()
{
    //declare identifiers 
    double rate = 1; 
    double time = 2;
    float dollars;
    float total=0.0; 

    //initialize identifiers
    total= pow(exp(1.0), rate*time); 

    cout << fixed << showpoint << setprecision(2);

    //Enter values
    cout << "Enter the money you want to deposit now(P): " << dollars;
    cout << "Enter the interest rate: " << rate;
    cout << "Enter duration in year(t): " << time;
    cout << "   Your balance after 5 years is " << total; 

    cout << endl; 

    //terminate program
    system("pause");
    return 0;
}

Dani AI

Generated

Quick summary and fixes (ties to posts by , and ):

  • The core runtime problems in the original snippet are I/O and initialization: prompts were printed but the program never actually read values into the variables. ’s code also used pow(exp(1.0), ...) which is equivalent to exp(...) but less clear. pointed to the right formula, but used old/non-portable headers and clrscr()/getch(); prefer standard C++ headers and std::cin/std::cout. is correct that continuous compounding is theoretical for many bank products — use the discrete formula when appropriate.

Minimal, portable example (modern C++):

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

int main() {
    double P, rate_percent, years;
    std::cout << "Principal (P): ";
    if (!(std::cin >> P)) return 0;
    std::cout << "Annual rate (%) : ";
    std::cin >> rate_percent;
    std::cout << "Time in years: ";
    std::cin >> years;

    double r = rate_percent / 100.0;              // percent -> decimal
    double amount = P * std::exp(r * years);      // continuous compounding

    std::cout << std::fixed << std::setprecision(2)
              << "Balance: " << amount << '\n';
}

Troubleshooting and notes:

  • Ensure you convert percent to decimal (5% -> 0.05). If you enter rate as 5 without conversion you’ll get an enormous result.
  • For discrete compounding use A = P (1 + r/n)^(nt) where n is periods per year.
  • Use double for calculations; avoid float for money. For production finance code, prefer integer cents or a decimal/fixed library to avoid rounding errors.
  • Avoid non-portable headers (<conio.h>, <graphics.h>) and system("pause"); use standard I/O and std::cin.get() if you need to pause.

Recommended Answers

All 3 Replies

Why do you want to continuously compound interest? It is theoretical text book stuff, in practice, it should be simple interest formula.

Ya I got it nevermind, thanks anyways.

1.#include <iostream.h>
2.#include <conio.h>
3.#include <math.h>
4.#include <iomanip.h>
5.#include <graphics.h>
6.//using namespace std;
7.int main()
8.{
9    clrscr();
10    //declare identifiers
11    double rate;
12    double time;
13    float dollars;
14    float total;
15    //initialize identifiers
16    cout << setprecision(2);
17    //Enter values
18    cout << "Enter the money you want to deposit now(P): ";
19    cin >> dollars;
20    cout << "Enter the interest rate: ";
21    cin >> rate;
22    cout << "Enter duration in year(t): ";
23    cin >> time;
24    total= dollars * exp(rate*time);
25    cout << "Your balance after the end of the given year is " << total;
26    //terminate program
27    getch();
28    return 0;
29}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.